# Import file in Python

**URL:** <https://discourse.mcneel.com/t/import-file-in-python/177564>\
**Category:** Grasshopper\
**Tags:** windows\
**Created:** [March 8, 2024, 6:40pm UTC](https://discourse.mcneel.com/t/import-file-in-python/177564 "2024-03-08T18:40:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_S](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/marcin_s/32/550496_2.png) [@Marcin\_S](https://discourse.mcneel.com/u/Marcin_S)\
**Post date:** [March 8, 2024, 6:40pm UTC](https://discourse.mcneel.com/t/import-file-in-python/177564/1 "2024-03-08T18:40:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![John\_Brock](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/john_brock/32/476283_2.png) [@John\_Brock](https://discourse.mcneel.com/u/John_Brock)\
**Post date:** [March 8, 2024, 6:41pm UTC](https://discourse.mcneel.com/t/import-file-in-python/177564/2 "2024-03-08T18:41:37Z")

</div>

Assigned to Scripting category

---

<div class="post-metadata">

**Author:** ![Marcin\_Smolnik](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/marcin_smolnik/32/555493_2.png) [@Marcin\_Smolnik](https://discourse.mcneel.com/u/Marcin_Smolnik)\
**Post date:** [March 8, 2024, 9:19pm UTC](https://discourse.mcneel.com/t/import-file-in-python/177564/3 "2024-03-08T21:19:30Z")

</div>

Done it in the end with help of chat GPT 🙂

#import sys  
#sys.path.append(r’D:\DIR’)  
#import file1

---

<div class="post-metadata">

**Author:** ![dn.aur](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@dn.aur](https://discourse.mcneel.com/u/dn.aur)\
**Post date:** [March 9, 2024, 11:44am UTC](https://discourse.mcneel.com/t/import-file-in-python/177564/4 "2024-03-09T11:44:42Z")

</div>

Solution without adding sy.path.append(…) each time

1. Run this in a Python node inside Grasshopper

```auto
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`

1. Find the file named `distutils-precedence.pth`

Open it with the editor of your choice, where you will find something like

```auto
import os; var = 'SETUPTOOLS_USE_DISTUTILS'; enabled = os.environ.get(var, 'stdlib') == 'local'; enabled and __import__ ('_distutils_hack').add_shim();

```

1. Add this in the next line

```auto
import sys; sys.path.append(r"D:\proton\My files\grasshopper\python")

```

1. Restart Rhino and verify

```auto
import sys
paths = sys.path

for path in paths:
    print(path)

```

1. Add your \*.py Files

 ![grafik](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/a/e/2/ae28b5121d1aa3a3222d1957100b794025193a1d.png)

1. Enjoy

 ![grafik](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/7/6/9/769dfac239dfb579921fe5a97af1da5e37f76aab.png)

---

<div class="post-metadata">

**Author:** ![Jonathan\_Garrison](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jonathan_garrison/32/665193_2.png) [@Jonathan\_Garrison](https://discourse.mcneel.com/u/Jonathan_Garrison)\
**Post date:** [April 25, 2024, 6:11pm UTC](https://discourse.mcneel.com/t/import-file-in-python/177564/5 "2024-04-25T18:11:46Z")

</div>

@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)

```

---

<div class="post-metadata">

**Author:** ![scottd](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/scottd/32/494002_2.png) [@scottd](https://discourse.mcneel.com/u/scottd)\
**Post date:** [April 25, 2024, 7:22pm UTC](https://discourse.mcneel.com/t/import-file-in-python/177564/6 "2024-04-25T19:22:06Z")

</div>

This also may work for you?

> [@How can you reload the CPython engine from python code](https://discourse.mcneel.com/t/how-can-you-reload-the-cpython-engine-from-python-code/180074/5):
>
> 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.
