配置¶
pip 允许用户通过 3 种机制来改变其行为:
命令行选项
环境变量
配置文件
本页解释了配置文件和环境变量如何工作,以及它们与 pip 的各种命令行选项的关系。
配置文件¶
配置文件可以改变命令行选项的默认值。它们是用标准的 INI 风格的配置文件编写的。
pip有 3 个 “级别” 的配置文件。
global
:全系统的配置文件,用户之间共享。user
:每个用户的配置文件。site
:每个环境的配置文件;即每个虚拟环境。
位置¶
pip 的配置文件位于相当标准的位置。这个位置在不同的操作系统上是不同的,而且由于向后兼容的原因,有一些额外的复杂性。
- 全局
在环境变量
XDG_CONFIG_DIRS
(如果存在的话)中设置的任何路径的 “pip” 子目录中,例如/etc/xdg/pip/pip.conf
。随后将加载
/etc/pip.conf
。- 用户
$HOME/.config/pip/pip.conf
,它遵从XDG_CONFIG_HOME
环境变量。传统的 “per-user” 配置文件也被加载,如果它存在的话:
$HOME/.pip/pip.conf
。- 站点
$VIRTUAL_ENV/pip.conf
- 全局
/Library/Application Support/pip/pip.conf
- 用户
$HOME/Library/Application Support/pip/pip.conf
如果目录存在$HOME/Library/Application Support/pip
否则$HOME/.config/pip/pip.conf
传统的 “per-user” 配置文件也被加载,如果它存在的话:
$HOME/.pip/pip.conf
。- 站点
$VIRTUAL_ENV/pip.conf
- 全局
在 Windows 7 和更高版本中:
C:\ProgramData\pip\pip.ini
(隐藏但可写)在 Windows Vista 上:不支持全局配置。
在 Windows XP 上:
C:\Documents and Settings\All Users\Application Data\pip\pip.ini
- 用户
%APPDATA%\pip\pip.ini
传统的 “per-user” 配置文件也被加载,如果它存在的话:
%HOME%\pip\pip.ini
- 站点
%VIRTUAL_ENV%\pip.ini
PIP_CONFIG_FILE
¶
此外,环境变量 PIP_CONFIG_FILE
可以用来指定一个首次加载的配置文件,其值被上述文件中设置的值所覆盖。将其设置为 os.devnull
,可以禁止加载 所有 配置文件。
载入顺序¶
当发现多个配置文件时,pip 会按照以下顺序组合它们:
PIP_CONFIG_FILE
,若被给定全局
用户
站点
每个文件的读取都会覆盖从以前的文件中读取的任何值,所以如果全局文件和每个用户文件中都指定了全局超时,那么将使用后者的值。
Naming¶
The names of the settings are derived from the long command line option.
As an example, if you want to use a different package index (--index-url
) and
set the HTTP timeout (--default-timeout
) to 60 seconds, your config file would
look like this:
[global]
timeout = 60
index-url = https://download.zope.org/ppix
Per-command section¶
Each subcommand can be configured optionally in its own section. This overrides the global setting with the same name.
As an example, if you want to decrease the timeout
to 10
seconds when
running the pip freeze, and use 60
seconds for all other commands:
[global]
timeout = 60
[freeze]
timeout = 10
Boolean options¶
Boolean options like --ignore-installed
or --no-dependencies
can be set
like this:
[install]
ignore-installed = true
no-dependencies = yes
To enable the boolean options --no-compile
, --no-warn-script-location
and
--no-cache-dir
, falsy values have to be used:
[global]
no-cache-dir = false
[install]
no-compile = no
no-warn-script-location = false
Repeatable options¶
For options which can be repeated like --verbose
and --quiet
, a
non-negative integer can be used to represent the level to be specified:
[global]
quiet = 0
verbose = 2
It is possible to append values to a section within a configuration file. This
is applicable to appending options like --find-links
or --trusted-host
,
which can be written on multiple lines:
[global]
find-links =
http://download.example.com
[install]
find-links =
http://mirror1.example.com
http://mirror2.example.com
trusted-host =
mirror1.example.com
mirror2.example.com
This enables users to add additional values in the order of entry for such command line arguments.
Environment Variables¶
pip’s command line options can be set with environment variables using the
format PIP_<UPPER_LONG_NAME>
. Dashes (-
) have to be replaced with
underscores (_
).
PIP_DEFAULT_TIMEOUT=60
is the same as--default-timeout=60
PIP_FIND_LINKS="http://mirror1.example.com http://mirror2.example.com"
is the same as
--find-links=http://mirror1.example.com --find-links=http://mirror2.example.com
Repeatable options that do not take a value (such as --verbose
) can be
specified using the number of repetitions:
PIP_VERBOSE=3
is the same aspip install -vvv
注解
Environment variables set to an empty string (like with export X=
on Unix) will not be treated as false.
Use no
, false
or 0
instead.
Precedence / Override order¶
Command line options have override environment variables, which override the values in a configuration file. Within the configuration file, values in command-specific sections over values in the global section.
Examples:
--host=foo
overridesPIP_HOST=foo
PIP_HOST=foo
overrides a config file with[global] host = foo
A command specific section in the config file
[<command>] host = bar
overrides the option with same name in the[global]
config file section.