Adding a custom directory to PYTHONPATH

I am trying to add a user directory to PYTHONPATH by following the tips on this post. Constantly adding a directory to PYTHONPATH . I use bash on Mac, if relevant. This is what I did:

  • open ~/.bash_profile
  • export PYTHONPATH="${PYTHONPATH}:/Users/Zhengnan/Library/Python/2.7/lib/python/site-packages" and save
  • source ~/.bash_profile

There were two problems:

  • When I ran sys.path inside the Python IDE, the intended directory was still not displayed.
  • When I ran Python in Terminal and ran sys.path there, the dir did appear, but all the other directories didn't match what I got in the previous step.

In particular, this is what I got from running sys.path inside the IDE. Cannot find the intended directory.

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 /.ip ython ']

And this is what I got from running sys.path from Terminal. The intended directory is the third item on 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 directory to PYTHONPATH is that every time I pip install package, it installs in /Users/Zhengnan/Library/Python/2.7/lib/python/site-packages , and I don't want sys.path.append every time I ran a script. Please advise. Thanks.

+5
source share
1 answer

A lot is going on here.

Essentially, the Python that you use in your IDE is not the Python that you use in the terminal. This is why pip install doesn't put things in the right place.

The simplest solution is to change the $PATH environment variable so that when you type python in the terminal, you get the same version as your IDE. I suppose your python IDE will be something like /Applications/Spyder-Py2.app/Contents/Resources/bin/python , in which case you will get rid of your PYTHONPATH by setting your .bash_profile and add:

 export PATH="/Applications/Spyder-Py2.app/Contents/Resources/bin:$PATH" 

Assuming pip available in the same place, you should now pip install use things without having to cheat with PYTHONPATH .

Another problem is that the environment variables set in the shell, for example in your .bash_profile , do not affect the environment visible to applications. You can set environment variables that will be visible to OS X applications (for example, see this question ), but this is complicated, and I would not recommend this.

An alternative solution, if available, is to simply tell your IDE that python should use, and specify it depending on what is visible from the terminal.

+2
source

Source: https://habr.com/ru/post/1247463/


All Articles