pyrevitlabs / pyRevit

Rapid Application Development (RAD) Environment for Autodesk Revit®
http://wiki.pyrevitlabs.io
GNU General Public License v3.0
1.31k stars 334 forks source link

CPython Issue with pathlib #1287

Closed CyrilWaechter closed 1 year ago

CyrilWaechter commented 3 years ago

Describe the bug There is an issue with CPython engine inside Revit only. It looks like it uses an old version of pathlib where exist_ok did not exist. This feature was apparently introduced in python 3.5.

To Reproduce Steps to reproduce the behavior:

  1. Create a script containing following code:
    
    #! python3
    from pathlib import Path

Path("test").mkdir(parents=True, exist_ok=True)

2. Execute this script
3. See error:
```python
CPython Traceback:
TypeError : mkdir() got an unexpected keyword argument 'exist_ok'
 File "<path_to_script_folder>\script.py", line 8, in <module>
Path("test").mkdir(parents=True, exist_ok=True)

pyRevitLabs.PythonNet
à Python.Runtime.Runtime.CheckExceptionOccurred()
 à Python.Runtime.PyScope.Exec(String code, IntPtr _globals, IntPtr _locals)
 à PyRevitLabs.PyRevit.Runtime.CPythonEngine.Execute(ScriptRuntime& runtime)

Expected behavior This should work like when you run it in the python engine directly:

<path_to_pyRevit>\pyRevit\bin\engines\CPY385 (master -> origin)
λ python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
>>> from pathlib import Path
>>> Path("test").mkdir(parents=True, exist_ok=True)
>>>

Screenshots image

Desktop (please complete the following information):

CyrilWaechter commented 3 years ago

I ran:

import pathlib

print(pathlib)

and got:

<module 'pathlib' from '<path_to_pyrevit>\\pyRevit\\site-packages\\pathlib.py'>

This explains why it does not use correct library as the engine ran separately:

<path_to_pyrevit>\pyRevit\bin\engines\CPY385 (master -> origin)
λ python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
>>> import pathlib
>>> pathlib
<module 'pathlib' from '<path_to_pyrevit>\\pyRevit\\bin\\engines\\CPY385\\python38.zip\\pathlib.pyc'>
>>>
eirannejad commented 3 years ago

so if you remove the pyRevit\site-packages\pathlib.py then it works? I need to separate these site-packages libraries for IronPython and Cpython

CyrilWaechter commented 3 years ago

so if you remove the pyRevit\site-packages\pathlib.py then it works? I need to separate these site-packages libraries for IronPython and Cpython

As extension’s lib folder stands before pyrevit sites-packages in sys.path I put pathlib.pyc from pyRevit\\bin\\engines\\CPY385\\python38.zip\\pathlib.pyc in extension’s lib folder but yes it works as it would probably with your suggestion. A solution could be to change sys.path order. I tried to found where all path are appended in pyRevit source code but did not figured all of it out.


import sys; import pprint; pprint.pprint(sys.path)

image