简体   繁体   English

easy_install或pip作为受限用户?

[英]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 distutils提供了一个'--user'选项 ,允许我以有限的用户身份安装包,如下所示:

python setup.py install --user

Is there an equivalent for easy_install and pip ? 是否有easy_installpip的等价物?

For pip , see User Installs for details, but basically, it's just what you'd expect: 有关pip ,请参阅用户安装以获取详细信息,但基本上,这正是您所期望的:

pip install --user Foo

It's a bit trickier for easy_install . 这对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 . 正如Ned Deily指出的那样,如果你可以依赖distribute而不是setuptools ,以及0.6.11或更高版本,你可以使用--userpip 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). 但是,如果您需要使用setuptools或较旧的distribute ...请参阅自定义安装位置以获取详细信息(并注意它解释了如何创建和设置用户站点包,而不仅仅是如何在那里安装,因为它需要能够工作使用Python 2.5及更早版本,默认情况下不执行此操作)。 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. 但我们希望,你只使用easy_install为未包装的少数pip -able,所以这不是什么大不了的事。

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. 但是,至少值得考虑virtualenv是否比用户站点目录更适合您要完成的任何事情。 pip and virtualenv work together very nicely, as the docs explain. 正如文档所解释的那样 pipvirtualenv很好地协同工作。

From the easy_install docs 来自easy_install文档

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

--install-dir=DIR, -d DIR Set the installation directory. --install-dir = DIR,-d DIR设置安装目录。 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. 您可以确保此目录在运行时位于sys.path上,并使用pkg_resources.require()来启用所需的已安装软件包。

(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. (0.4a2中的新增内容)如果未在命令行或distutils配置文件中直接指定此选项,则使用distutils默认安装位置。 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. 通常,这将是site-packages目录,但是如果您使用distutils配置文件,设置诸如prefix或install_lib之类的东西,那么在计算默认安装目录时会考虑这些设置,就像--prefix选项一样。

--prefix=DIR (New in 0.6a10) Use the specified directory as a base for computing the default installation and script directories. --prefix = DIR(0.6a10中的新增内容)使用指定的目录作为计算默认安装和脚本目录的基础。 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. 在Windows上,生成的默认目录将是前缀\\ Lib \\ site-packages和前缀\\ Scripts,而在其他平台上,默认目录将是前缀/ lib / python2.X / site-packages(替换了相应的版本),用于库和脚本的前缀/ bin。

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. 请注意, - prefix选项仅设置默认安装和脚本目录,并且不会覆盖在命令行或配置文件中设置的目录。

You can also specify them on using a ~/.pydistutils.cfg file 您也可以使用〜/ .pydistutils.cfg文件指定它们

http://peak.telecommunity.com/DevCenter/EasyInstall#mac-os-x-user-installation 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): 在安装EasyInstall / setuptools之前,只需创建一个包含以下内容的〜/ .pydistutils.cfg文件(或将其添加到现有内容):

[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. [install] install_lib =〜/ Library / Python / $ py_version_short / site-packages install_scripts =〜/ bin这将告诉distutils和EasyInstall始终在个人site-packages目录中安装软件包,并将脚本安装到〜/ 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.) (注意:不要将$ py_version_short替换为配置文件中的实际Python版本!distutils将在运行时替换正确的值,以便上述配置文件无论您现在还是将来使用的Python版本都能正常工作。)

Once you have done this, you can follow the normal installation instructions and use easy_install without any other special options or steps. 完成此操作后,您可以按照正常的安装说明操作,并使用easy_install,而无需任何其他特殊选项或步骤。

(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. (但请注意,〜/ bin不在默认路径中,因此您可能必须按其完整位置引用脚本。您可能需要修改shell启动脚本(可能是.bashrc或.profile)或者〜/ .MacOSX / environment.plist在您的PATH中包含〜/ bin。

I needed to do the same with my docker and deploy to AWS Elastic Beanstalk. 我需要对我的docker做同样的事情并部署到AWS Elastic Beanstalk。

The issue is that pip and easy_install (called by python setup.py) interpret the --user parameter in different way. 问题是pipeasy_install (由python setup.py调用)以不同的方式解释--user参数。

  • pip install --user PackageName will install the PackageName to $PYTHONUSERBASE environment variable. pip install --user PackageNamepip install --user PackageName安装到$PYTHONUSERBASE环境变量。
  • python setup.py develop --user will ignore $PYTHONUSERBASE variable and always install to ~/.local/lib/python<python version>/site-packages folder python setup.py develop --user将忽略$PYTHONUSERBASE变量并始终安装到~/.local/lib/python<python version>/site-packages文件夹

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 我发现这两个文件夹一起工作的唯一方法就是删除~/.local/lib/python<python version>/site-packages并链接到你的$PYTHONUSERBASE

PS: Be careful with bellow, you may need to reinstall all your local python dependencies. PS:小心下面,你可能需要重新安装所有本地python依赖项。 I recommend use it only in docker or any other virtual environment 我建议仅在docker或任何其他虚拟环境中使用它

# !!! 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 现在pip和python setup.py devel --user都将安装到同一个文件夹中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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