Pip配置设置不适用于虚拟环境



学习https://pip.pypa.io/en/stable/topics/configuration/我知道我可以有多个pip.conf文件(在基于UNIX的系统上(,这些文件按所述顺序加载。

我的任务是编写一个bash脚本,自动创建一个虚拟环境,并仅为虚拟环境设置pip配置。

# my_bash_script.sh
...
python -m virtualenv .myvenv
....
touch pip.conf
# this will create path/to/.myvenv/pip.conf
# otherwise following commands will be in the user's pip.conf at ~/.config/pip/pip.conf
path/to/.myvenv/bin/python -m pip config set global.proxy "my-company-proxy.com"
# setting our company proxy here
path/to/.myvenv/bin/python -m pip config set global.trusted-host "pypi.org pypi.python.org files.pythonhosted.org" 
# because of SSL issues from behind the company's firewall I need this to make pip work
...

我的问题是,我不想为global设置配置,而是为site设置配置。如果我用global.proxyglobal.trusted-host交换site.proxysite.trusted-host,pip将无法再安装软件包,而如果我把它留在global,一切都很好。将其更改为install.proxyinstall.trusted-host也不起作用。

pip.conf文件之后如下所示:

# /path/to/.myvenv/pip.conf
[global]
proxy = "my-company-proxy.com"
trusted-host = "pypi.org pypi.python.org files.pythonhosted.org"

pip config debug产生以下结果:

env_var:
env:
global:
/etc/xdg/pip/pip.conf, exists: False
/etc/pip.conf, exists: False
site:
/path/to/.myvenv/pip.conf, exists: True
global.proxy: my-company-proxy.com
global.trusted-host: pypi.org pypi.python.org files.pythonhosted.org
user:
/path/to/myuser/.pip/pip.conf, exists: False
/path/to/myuser/.config/pip/pip.conf, exists: True

我在这里错过了什么?提前感谢您的帮助!

配置文件中的[global]表示这些设置用于所有pip命令。请参阅本手册的本节。所以你可以做一些类似的事情

[global]
timeout = 60
[freeze]
timeout = 10

global/site的区别来自配置文件的位置。因此,您的文件/path/to/.myvenv/pip.conf通过其位置被称为site配置文件。在里面,你仍然需要有

[global]
proxy = "my-company-proxy.com"
trusted-host = "pypi.org pypi.python.org files.pythonhosted.org"

最新更新