简体   繁体   中英

What's the standard way to install modules from the Python Standard Library into a local directory?

I've found that the admin of the server I'm using tends to leave python modules extremely outdated and I don't have time to wait for this person to update the global directories.

Is there any way I can have my local python modules take precedence over the global libraries without manually modifying the sys.path?

There are various options. If you are using Python 2.6 or later, you can install Python modules in your home directory using the user installation scheme supported by Python's Distutils. Just add --user to the setup.py install command as described in the link. Another popular option is to use the third-party virtualenv package to create one or more isolated local Python environments.

UPDATE: For the user installation scheme , checking for the user site-packages directory is done during Python startup. If the directory exists, it is added to sys.path prior to the system site-packages directory. For example, with a Debian Linux installation:

$ python2.7 -c 'import site; print(site.USER_SITE)'
/home/nad/.local/lib/python2.7/site-packages
$ ls -l /home/nad/.local/lib/python2.7/site-packages
ls: cannot access /home/nad/.local/lib/python2.7/site-packages: No such file or directory
$ python2.7 -c 'import sys; print(sys.path)'
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7']
# user site-packages dir doesn't exist so it's not in sys.path
$ mkdir -p ~/.local/lib/python2.7/site-packages
$ python2.7 -c 'import sys; print(sys.path)'
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/nad/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7']
# now user site-packages dir exists so it is in sys.path and before /usr/lib ones

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM