Sub topic 13
Configuring pip and Using Alternative Package Indexes
This chapter assumes you can create and activate a virtual environment, and that you run pip as python -m pip inside it. It shows how to change pip's default behavior and how to install from somewhere other than the public Python Package Index (PyPI).
What a package index is
A package index is a web service that lists packages and their downloadable files. By default pip uses PyPI at https://pypi.org/simple. Companies often run a private index, such as an Artifactory or devpi server, for internal packages or as a mirror of PyPI.
Three ways to set an option
pip reads each option from three places. When the same option appears in more than one, the more specific source wins.
- Command-line flags, such as
--index-url. - Environment variables, named
PIP_plus the option name in upper case, such asPIP_INDEX_URL. - Configuration files.
Configuration files
pip has three configuration scopes:
- global: applies to every user on the machine.
- user: applies to your account, for example
~/.config/pip/pip.confon Linux or%APPDATA%\pip\pip.inion Windows. - site: applies only to the current virtual environment, stored as
pip.conf(pip.inion Windows) inside the environment directory.
Site settings override user settings, and user settings override global ones. Use the pip config commands instead of editing files by hand:
python -m pip config list
python -m pip config debug
python -m pip config set global.timeout 30
python -m pip config --site set global.index-url https://pypi.example.com/simple
python -m pip config unset global.timeoutconfig list shows the values pip is using. config debug shows which files pip checked and whether each exists, which is the quickest way to find out why a setting is not taking effect. The --site flag writes the setting into the active environment only.
A configuration file is a plain INI file. The section name is global for options shared by all commands, or a command name such as install:
[global]
timeout = 30
[install]
index-url = https://pypi.example.com/simpleChoosing an index
Three options control where pip finds packages:
| Option | Effect |
|---|---|
| `--index-url` (`-i`) | Replaces PyPI with a different index. |
| `--extra-index-url` | Searches an additional index alongside the main one. |
| `--find-links` (`-f`) | Looks in a directory or web page of package files. |
python -m pip install --index-url https://pypi.example.com/simple internal-tools
python -m pip install --no-index --find-links ./wheels requestsThe second command installs without any network index. --no-index stops pip from contacting an index, and --find-links ./wheels supplies a local folder of .whl or .tar.gz files. This is useful on machines with no internet access. Dependencies must be in that folder too, or the install fails.
You can also set the index for a single shell session:
export PIP_INDEX_URL=https://pypi.example.com/simple
python -m pip install requestsCommon errors
- A setting seems ignored. A higher-priority source is overriding it. Run
python -m pip config debugand check for aPIP_*environment variable. - A package is not found on a private index. The index may only contain internal packages. Public dependencies then need a mirror of PyPI or
--extra-index-url. - Timeouts or certificate errors behind a proxy. Set
--proxy, orPIP_PROXY, and make sure the proxy's certificate is trusted.
Do not put passwords directly in a URL stored in a file that you commit to version control. Use a credential helper such as the keyring package, or provide the URL through a secret environment variable in your CI system.
Exercise
Your team's pip.conf sets index-url to a private server, but a colleague's install still reaches pypi.org. Name two places to check and the command that helps.
Show answer
Check for a PIP_INDEX_URL environment variable or an --index-url flag in the install command, since both override configuration files. Also check whether a higher-priority file, such as the site pip.conf in the virtual environment, sets a different value. Run python -m pip config debug to see which files and values pip is reading.