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?
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)"
Source: Stackoverflow.com