学习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.proxy
和global.trusted-host
交换site.proxy
和site.trusted-host
,pip将无法再安装软件包,而如果我把它留在global
,一切都很好。将其更改为install.proxy
和install.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"