I've been trying to add a custom directory to PYTHONPATH
following the advice on this post Permanently add a directory to PYTHONPATH. I'm using bash on a Mac, if that's relevant. This is what I did:
open ~/.bash_profile
export PYTHONPATH="${PYTHONPATH}:/Users/Zhengnan/Library/Python/2.7/lib/python/site-packages"
and savesource ~/.bash_profile
There were two problems:
- When I ran
sys.path
inside a Python IDE, the intended dir still didn't show up. - When I fired up Python in Terminal and ran
sys.path
there, the dir did show up, but all the other directories didn't match what I got from the previous step.
Specifically, this is what I got from running sys.path
inside the IDE. The intended dir couldn't be found.
sys.path ['', '/Applications/Spyder-Py2.app/Contents/Resources', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python27.zip', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/plat-darwin', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/plat-mac', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/plat-mac/lib-scriptpackages', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/lib-tk', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/lib-old', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/lib-dynload', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/site-packages.zip', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/site-packages', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/IPython/extensions', '/Users/Zhengnan/.ipython']
And this is what I got from running sys.path
from Terminal. The intended dir is the third element in the list.
sys.path ['', '/Users/Zhengnan', '/Users/Zhengnan/Library/Python/2.7/lib/python/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
I should mention that the reason I want to add this custom dir to PYTHONPATH
is that every time I pip install
a package, it gets installed in /Users/Zhengnan/Library/Python/2.7/lib/python/site-packages
and I don't want to sys.path.append
every time I run a script. Please advise. Thanks.