Marcin_S
(Marcin S)
March 8, 2024, 6:40pm
1
Hi sorry for simple question, but I spend a lot of time and just can’t do it.
I want to import file “file1.py” from directory d:\directory\ to python script in GH file
I dont know why when I copy my file (file1.py) to the same directory (d:\directory) as my grasshopper.gh file and type
" from file1 import * "
I cannot load file1.py to my python script.
I got error "no module named ‘file1’.
Maybe I dont know where to copy my file?
Assigned to Scripting category
1 Like
Done it in the end with help of chat GPT
import sys
#sys .path.append(r’D:\DIR’)
import file1
1 Like
dn.aur
(Dan)
March 9, 2024, 11:44am
4
Solution without adding sy.path.append(…) each time
Run this in a Python node inside Grasshopper
import site
sps = site.getsitepackages()
for sp in sps:
print(sp)
The output should look something like this
C:\Users\x\.rhinocode\py39-rh8
C:\Users\x\.rhinocode\py39-rh8\lib\site-packages
Find the file named distutils-precedence.pth
Open it with the editor of your choice, where you will find something like
import os; var = 'SETUPTOOLS_USE_DISTUTILS'; enabled = os.environ.get(var, 'stdlib') == 'local'; enabled and __import__('_distutils_hack').add_shim();
Add this in the next line
import sys; sys.path.append(r"D:\proton\My files\grasshopper\python")
Restart Rhino and verify
import sys
paths = sys.path
for path in paths:
print(path)
Add your *.py Files
Enjoy
1 Like
@dn.aur Thank you so much for documenting this technique!
Just a small note, if you want to actively develop the referenced python modules you’ll need to reload the module, like so:
import myExternalPythonFile
from importlib import reload
reload(myExternalPythonFile)
1 Like
scottd
(Scott Davidson)
April 25, 2024, 7:22pm
6
This also may work for you?
We have been working on this a bit.
Put this at the top of the script:
# flag: python.reloadEngine
That will reset the engine each time it runs. This can take some extra time to reload, but does allow libraries to be reloaded if those happen to be changing often.
1 Like