Directing Python to look in another folder for modules

I'm still pretty new to python so forgive me if this is extremely simple or extremely the wrong way of thinking about this.

I have python 2.7 installed. From what I understand when I run the following code, it lists the directories where it looks for modules.

Python 2.7.12 (default, Oct 11 2016, 14:42:58) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print 'n'.join(sys.path)

/usr/local/lib/python2.7/site-packages
/usr/lib/python2.7/site-packages
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/readline
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

I have another directory that appears to have a bunch of python modules I installed in it. "/Library/Python/2.7/site-packages"

I guess I need to do one of two things:

(1) direct python to look in this additional folder for modules. How do I do that?

(2) install the modules in one of the folders it is already directing to. I've been using pip to install modules, and I think pip is installing to this additional director. How do I check if pip is installing to this folder? How do I change where pip install packages to?

Thanks!


This stuff is setup by site.py . You can find out which site.py by entering the interpreter with:

python -v

Alternatively, import site in the interactive interpreter and check the site.__file__ attribute.

There's also a helpful script in site.py which you can run with

python -m site

What you want to see in the output there is your user site, something like

USER_BASE: '/home/<your_username>/.local' (exists)
USER_SITE: '/home/<your_username>/.local/lib/python2.7/site-packages' (exists)
ENABLE_USER_SITE: True

When you pip install a package, use pip install --user , which will install modules to your user space. Don't install stuff with sudo pip install . Don't munge the sys.path manually like the "quick fix" from jmd_dk suggests. Do it properly.

If, after reading the site documentation and PEP370, you are still having trouble setting up the user site correctly, then it is acceptable to add a line like this in your bash profile:

export PYTHONPATH=/home/<your_username>/.local/lib/python2.7/site-packages

If you have both a Python 3 and a Python 2 installation, beware that the PYTHONPATH environment variable will be seen by both interpreters. In this case, it is strongly recommend to enable the site.USER_SITE separately for each interpreter (or use virtual environments) to provide adequate namespacing of installed packages.

链接地址: http://www.djcxy.com/p/87184.html

上一篇: Python冲突Numpy vs Scipy

下一篇: 指示Python在另一个文件夹中查找模块