Pip 本身是一个 Python 包,实际的 pip 命令只是运行一个小的 Python 脚本,然后导入并运行 pip 包。
您可以编辑 locations.py 以更改安装目录,但是,如上所述,我强烈建议您不要这样做。
Pip 命令
Pip 接受一个标志,'--install-option="--install-scripts"',可用于更改安装目录:
pip install somepackage --install-option="--install-scripts=/usr/local/bin"
来源方法
在 pip/locations.py 的第 124 行,我们看到以下内容:
site_packages = sysconfig.get_python_lib()
user_site = site.USER_SITE
您可以在技术上编辑这些以更改默认安装路径,但是,最好使用虚拟环境。然后用它来查找 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
然而,再一次,使用 virtualenv 更容易追踪,任何 Pip 更新都将覆盖这些更改,这与您自己的 virtualenv 不同。