简单安装还是作为有限用户使用pip?

2024-05-16 12:19:29 发布

您现在位置:Python中文网/ 问答频道 /正文

标准pythondistutils provides a '--user' option允许我以有限用户身份安装包,如下所示:

python setup.py install --user

是否有与“轻松安装”和“pip”等效的选项?


Tags: installpip用户py标准选项setup身份
3条回答

我需要对docker做同样的操作并部署到AWS弹性Beanstalk。

问题是pipeasy_install(由python setup.py调用)以不同的方式解释--user参数。

  • pip install --user PackageName将PackageName安装到$PYTHONUSERBASE环境变量。
  • python setup.py develop --user将忽略$PYTHONUSERBASE变量,并始终安装到~/.local/lib/python<python version>/site-packages文件夹

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

注意下面的代码,您可能需要重新安装所有本地python依赖项。我建议只在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

现在pip和python setup.py devel——用户将安装到同一个文件夹

有关pip的详细信息,请参见User Installs,但基本上,这正是您所期望的:

pip install --user Foo

这对easy_install来说有点棘手。正如Ned Deily所指出的,如果你可以依赖于distribute,而不是setuptools,或者0.6.11或更高版本,你可以使用--userpip相同的方法。但是如果您需要使用setuptools,或者更旧的distribute……请参见Custom Installation Locations以了解详细信息(并注意,它解释了如何创建和设置用户站点包,而不仅仅是如何在那里安装,因为它需要能够使用Python 2.5和更早版本,而python2.5和更早版本在默认情况下没有这样做)。但是,希望您只对少数不支持pip的包使用easy_install,所以这不是什么大事。

然而,至少值得考虑的是^{}是否比用户站点目录更适合您要完成的任务。pipvirtualenv很好地协同工作,正如the 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.

也可以在使用~/.pydistutils.cfg文件时指定它们

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

在安装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. (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.

相关问题 更多 >