Question

I had a file called example_file.py, which I wanted to use from various other files, so I decided to add example_file.py to sys.path and import this file in another file to use the file. To do so, I ran the following in IPython.

import sys
sys.path
sys.path.append('/path/to/the/example_file.py')
print(sys.path)

I could see the path I had just added, and when I tried to import this file from another directory path like this:

import example_file

it worked just fine, but once I came out of IPython, entered it again, and checked the sys.path, I saw that the path which I had added was not present, so how do I add a path to sys.path permanently in Python?

Was it helpful?

Solution

There are a few ways. One of the simplest is to create a my-paths.pth file (as described here). This is just a file with the extension .pth that you put into your system site-packages directory. On each line of the file you put one directory name, so you can put a line in there with /path/to/the/ and it will add that directory to the path.

You could also use the PYTHONPATH environment variable, which is like the system PATH variable but contains directories that will be added to sys.path. See the documentation.

Note that no matter what you do, sys.path contains directories not files. You can't "add a file to sys.path". You always add its directory and then you can import the file.

OTHER TIPS

This way worked for me:

adding the path that you like:

export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add

checking: you can run 'export' cmd and check the output or you can check it using this cmd:

python -c "import sys; print(sys.path)"

In one windows distribution in the following file: <python_root_installation_directory>/python38._pth

there are the following lines:

python38.zip
.
./lib
./lib/site-packages

# Uncomment to run site.main() automatically
#import site

Thus, with this content there the following yields:

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
>>> import sys
>>> sys.path
['C:\\Program Files\\Applications\\python_3_8_2\\python38.zip', 'C:\\Program Files\\Applications\\python_3_8_2', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib/site-packages']

So After adding this line into the file: ./lib/site-packages/win32ctypes it is present in the path:

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
>>> import sys
>>> sys.path
['C:\\Program Files\\Applications\\python_3_8_2\\python38.zip', 'C:\\Program Files\\Applications\\python_3_8_2', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib/site-packages', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib/site-packages/win32ctypes']

This way, you don't need to have PYTHONPATH variable present on the system and you can still have the functionality. Disadvantage would be that this is installation specific, so if you have 3 different distributions on your system, this will affect only the chosen installation, while PYTHONPATH will affect all of them simultaneously.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top