How I set Python's USER_SITE; do I need to?

2019-06-02 23:53发布

问题:

I have a Python installation on OS X (10.10; maintained simply with pip) with my site packages in

/Library/Python/2.7/site-packages

Apple's packages in

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python

and the standard Apple Python installation in

/System/Library/Frameworks/Python.framework/Versions/2.7

I did nothing special to set this up, so I assume it is pretty standard. I install my packages in the site packages directory, and the only thing I have done to "customize" is to prune the Apple packages I don't need or that duplicate ones I maintain in my site packages directory.

Everything works fine, and all my paths seem to be just what I'd expect; however I get a confusing result when I

python -m site

While this shows a sys.path that makes sense to me

sys.path = [
    '/Users/Rax',
    '/Users/Rax/Documents/Projects/Coding/Python', # From PYTHONPATH
    '/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',
    '/Library/Python/2.7/site-packages',
]

I also get

USER_BASE: '/Users/Rax/Library/Python/2.7' (doesn't exist)
USER_SITE: '/Users/Rax/Library/Python/2.7/lib/python/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

which doesn't make sense to me.

Shouldn't USER_SITE be /Library/Python/2.7/site-packages? If so, how do I set it (I can't by setting USER_SITE to /Library/Python/2.7/ because lib/python/ is added to the path)?


This is doubly confusing to me because /Library/Python/2.7/site-packages is correctly added to my sys.path (from where I don't know) and because

import pkg_resources
pkg_resources.__file__

yields /Library/Python/2.7/site-packages/pkg_resources.pyc.

回答1:

The user-site is a machinery is meant "to allow users the local installation of Python packages in their home directory." (see here). i.e., it's meant for local user packages, rather than the site-wide installation. If you have multiple users on your system, their user site packages would be separate.

From the documentation, we can see that USER_SITE is:

Path to the user site-packages for the running Python. Can be None if getusersitepackages() hasn’t been called yet. Default value is ~/.local/lib/pythonX.Y/site-packages for UNIX and non-framework Mac OS X builds, ~/Library/Python/X.Y/lib/python/site-packages for Mac framework builds, and %APPDATA%\Python\PythonXY\site-packages on Windows. This directory is a site directory, which means that .pth files in it will be processed.

(The one for user-base is at the same location)

So to answer your question - I believe they're set up correctly. It's a different site packages location than the regular one.

You can also see in the general module documentation it says:

This file is intended to be created in the user site-packages directory (see below), which is part of sys.path unless disabled by -s. An ImportError will be silently ignored.

So the fact that these paths do not exist doesn't matter.

Also, regarding your question:

Shouldn't USER_SITE be /Library/Python/2.7/site-packages?

Note that in the docs it says ~/Library/... - that ~ is replaced with /Users/Rax/

You should not worry about the USER_SITE setting at all. If the directory doesn't exist it is not added to the path anyway.