Whenever I use sys.path.append
, the new directory will be added. However, once I close python, the list will revert to the previous (default?) values. How do I
On MacOS, Instead of giving path to a specific library. Giving full path to the root project folder in
~/.bash_profile
made my day, for example:
export PYTHONPATH="${PYTHONPATH}:/Users/<myuser>/project_root_folder_path"
after this do:
source ~/.bash_profile
Adding export PYTHONPATH="${PYTHONPATH}:/my/other/path"
to the ~/.bashrc might not work if PYTHONPATH
does not currently exist (because of the :
).
export PYTHONPATH="/my/other/path1"
export PYTHONPATH="${PYTHONPATH}:/my/other/path2"
Adding the above to my ~/.bashrc did the trick for me on Ubuntu 16.04
Shortest path between A <-> B is a straight line;
import sys
if not 'NEW_PATH' in sys.path:
sys.path += ['NEW_PATH']
I ran into Python Path problems when I switched to zsh from bash.
The solution was simple, but I failed to notice.
Pip was showing me, that the scripts blah blah
or package blah blah
is installed in ~/.local/bin
which is not in path.
After reading some solutions to this question, I opened my .zshrc
to find that the solution already existed.
I had to simply uncomment a line:
Take a look
Just to add on awesomo's answer, you can also add that line into your ~/.bash_profile
or ~/.profile
You could add the path via your pythonrc file, which defaults to ~/.pythonrc on linux. ie.
import sys
sys.path.append('/path/to/dir')
You could also set the PYTHONPATH
environment variable, in a global rc file, such ~/.profile
on mac or linux, or via Control Panel -> System -> Advanced tab -> Environment Variables on windows.