[英]How do I set the installation directory for pip?
How can I set the installation path for pip
using get-pip.py
to /usr/local/bin/
? 如何使用get-pip.py
为/usr/local/bin/
设置pip
的安装路径? I can't find any mention in the setup guide or in the command line options. 我在设置指南或命令行选项中找不到任何提及。
To clarify I don't mean the path where pip packages are installed, but the path where pip itself is installed (it shuold be in /usr/local/bin/pip
). 澄清我并不是指安装pip包的路径,而是安装pip本身的路径(它应该在/usr/local/bin/pip
)。
Edit 编辑
I do agree with many of the comments/answers that virtualenv would be a better idea in general. 我同意很多评论/答案,一般来说,virtualenv会是一个更好的主意。 However it simply isn't the best one for me at the moment since it would be too disruptive; 然而,它对我来说根本不是最好的,因为它太具有破坏性; many of our user's scripts rely on a python2.7 being magically available; 我们的许多用户脚本依赖于python2.7神奇可用; this is not convenient either and should change, but we have been using python since before virtualenv was a thing. 这也不方便也应该改变,但我们一直在使用python,因为之前virtualenv是一个东西。
Pip itself is a Python package, and the actual pip command just runs a small Python script which then imports and runs the pip package. Pip本身是一个Python包,实际的pip命令只运行一个小的Python脚本,然后导入并运行pip包。
You can edit locations.py to change installation directories, however, as stated above, I highly recommend that you do not do this. 您可以编辑locations.py来更改安装目录,但是,如上所述,我强烈建议您不要这样做。
Pip Command Pip命令
Pip accepts a flag, '--install-option="--install-scripts"', which can be used to change the installation directory: Pip接受一个标志'--install-option =“ - install-scripts”',可用于更改安装目录:
pip install somepackage --install-option="--install-scripts=/usr/local/bin"
Source method 来源方法
On line 124 in pip/locations.py, we see the following: 在pip / locations.py的第124行,我们看到以下内容:
site_packages = sysconfig.get_python_lib()
user_site = site.USER_SITE
You can technically edit these to change the default install path, however, using a virtual environment would be highly preferable. 您可以在技术上编辑这些以更改默认安装路径,但是,使用虚拟环境将是非常可取的。 This is then used to find the egglink path, which then finds the dist path (code appended below, from pip/__init__.py
). 然后用它来查找egglink路径,然后找到dist路径(下面的代码来自pip/__init__.py
)。
def egg_link_path(dist):
"""
Return the path for the .egg-link file if it exists, otherwise, None.
There's 3 scenarios:
1) not in a virtualenv
try to find in site.USER_SITE, then site_packages
2) in a no-global virtualenv
try to find in site_packages
3) in a yes-global virtualenv
try to find in site_packages, then site.USER_SITE
(don't look in global location)
For #1 and #3, there could be odd cases, where there's an egg-link in 2
locations.
This method will just return the first one found.
"""
sites = []
if running_under_virtualenv():
if virtualenv_no_global():
sites.append(site_packages)
else:
sites.append(site_packages)
if user_site:
sites.append(user_site)
else:
if user_site:
sites.append(user_site)
sites.append(site_packages)
for site in sites:
egglink = os.path.join(site, dist.project_name) + '.egg-link'
if os.path.isfile(egglink):
return egglink
def dist_location(dist):
"""
Get the site-packages location of this distribution. Generally
this is dist.location, except in the case of develop-installed
packages, where dist.location is the source code location, and we
want to know where the egg-link file is.
"""
egg_link = egg_link_path(dist)
if egg_link:
return egg_link
return dist.location
However, once again, using a virtualenv is much more traceable, and any Pip updates will override these changes, unlike your own virtualenv. 但是,再次使用virtualenv更易于追踪,并且任何Pip更新都将覆盖这些更改,这与您自己的virtualenv不同。
It seems the easiest workaround I found to do this is: 似乎我发现这样做的最简单的解决方法是:
easy_install
; 安装easy_install
; this will go in /usr/local/bin/
as expected; 这将按照预期进入/usr/local/bin/
; the steps for doing this are listed here ; 这里列出了执行此操作的步骤; I personally ended up running wget https://bootstrap.pypa.io/ez_setup.py -O - | python
我个人最终运行wget https://bootstrap.pypa.io/ez_setup.py -O - | python
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
/usr/local/bin/easy_install pip
; 使用/usr/local/bin/easy_install pip
安装/usr/local/bin/easy_install pip
; this will make pip
go in /usr/local/bin
这将使pip
进入/usr/local/bin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.