简体   繁体   中英

easy_install or pip as a limited user?

Standard python distutils provides a '--user' option which lets me install a package as a limited user, like this:

python setup.py install --user

Is there an equivalent for easy_install and pip ?

For pip , see User Installs for details, but basically, it's just what you'd expect:

pip install --user Foo

It's a bit trickier for easy_install . As Ned Deily points out, if you can rely on distribute rather than setuptools , and 0.6.11 or later, you can just use --user the same as pip . But if you need to work with setuptools , or older distribute … see Custom Installation Locations for details (and note that it explains how to create and set up user site packages, not just how to install there, because it needs to be able to work with Python 2.5 and earlier, which didn't do this by default). But hopefully, you're only using easy_install for the handful of packages that aren't pip -able, so that isn't a big deal.

However, it's at least worth considering whether virtualenv is a better fit for whatever you're trying to accomplish than a user site directory. pip and virtualenv work together very nicely, as the docs explain.

From the easy_install docs

http://peak.telecommunity.com/DevCenter/EasyInstall#downloading-and-installing-a-package

--install-dir=DIR, -d DIR Set the installation directory. It is up to you to ensure that this directory is on sys.path at runtime, and to use pkg_resources.require() to enable the installed package(s) that you need.

(New in 0.4a2) If this option is not directly specified on the command line or in a distutils configuration file, the distutils default installation location is used. Normally, this would be the site-packages directory, but if you are using distutils configuration files, setting things like prefix or install_lib, then those settings are taken into account when computing the default installation directory, as is the --prefix option.

--prefix=DIR (New in 0.6a10) Use the specified directory as a base for computing the default installation and script directories. On Windows, the resulting default directories will be prefix\\Lib\\site-packages and prefix\\Scripts, while on other platforms the defaults will be prefix/lib/python2.X/site-packages (with the appropriate version substituted) for libraries and prefix/bin for scripts.

Note that the --prefix option only sets the default installation and script directories, and does not override the ones set on the command line or in a configuration file.

You can also specify them on using a ~/.pydistutils.cfg file

http://peak.telecommunity.com/DevCenter/EasyInstall#mac-os-x-user-installation

Before installing EasyInstall/setuptools, just create a ~/.pydistutils.cfg file with the following contents (or add this to the existing contents):

[install] install_lib = ~/Library/Python/$py_version_short/site-packages install_scripts = ~/bin This will tell the distutils and EasyInstall to always install packages in your personal site-packages directory, and scripts to ~/bin. (Note: do not replace $py_version_short with an actual Python version in the configuration file! The distutils will substitute the correct value at runtime, so that the above configuration file should work correctly no matter what Python version you use, now or in the future.)

Once you have done this, you can follow the normal installation instructions and use easy_install without any other special options or steps.

(Note, however, that ~/bin is not in the default PATH, so you may have to refer to scripts by their full location. You may want to modify your shell startup script (likely .bashrc or .profile) or your ~/.MacOSX/environment.plist to include ~/bin in your PATH.

I needed to do the same with my docker and deploy to AWS Elastic Beanstalk.

The issue is that pip and easy_install (called by python setup.py) interpret the --user parameter in different way.

  • pip install --user PackageName will install the PackageName to $PYTHONUSERBASE environment variable.
  • python setup.py develop --user will ignore $PYTHONUSERBASE variable and always install to ~/.local/lib/python<python version>/site-packages folder

The only way I found for both these folders works together, is to delete ~/.local/lib/python<python version>/site-packages and to make link to your $PYTHONUSERBASE

PS: Be careful with bellow, you may need to reinstall all your local python dependencies. I recommend use it only in docker or any other virtual environment

# !!! Be sure you have configured $PYTHONUSERBASE environment variable
rm -r ~/.local/lib/python2.7/site-packages
ln -s $PYTHONUSERBASE/lib/python2.7/site-packages ~/.local/lib/python2.7/site-packages

Now both pip and python setup.py devel --user will install to the same folder

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