Sub topic 13
Configuring pip and using alternative package indexes
This chapter assumes you can create and activate a virtual environment, install packages with python -m pip, and read a requirements file. It covers where pip reads its settings, how those settings are ranked, and how to point pip at a package index other than PyPI.
Why configure pip
By default pip downloads from the Python Package Index (PyPI) at https://pypi.org/simple. Many teams need something different:
- A company mirror or private index that hosts internal packages.
- A proxy or a custom certificate authority (CA) bundle.
- A rule that stops anyone from installing into the system interpreter by accident.
- An offline machine that installs from a folder of files.
You can pass options on every command, but pip also lets you store them once. Both approaches use the same option names.
Three ways to set an option
pip accepts the same option from three sources. Take index-url as an example:
| Source | Example |
|---|---|
| Command line | `python -m pip install --index-url https://pypi.example.com/simple requests` |
| Environment variable | `PIP_INDEX_URL=https://pypi.example.com/simple` |
| Configuration file | `index-url = https://pypi.example.com/simple` |
The rule for turning an option into the other two forms is mechanical. Drop the leading dashes, then use the result as the config key. For the environment variable, upper-case the name, replace - with _, and add the PIP_ prefix. So --require-virtualenv becomes require-virtualenv in a file and PIP_REQUIRE_VIRTUALENV in the environment.
When the same option is set in more than one place, the more specific source wins:
The command line always wins. Environment variables beat every config file. Among the files, the site file (inside the active virtual environment) overrides the user file, and the user file overrides the global file. If you set the PIP_CONFIG_FILE environment variable, pip loads that file after the others, so it overrides them.
Configuration file locations
Each level has its own file. The names differ by platform: pip.conf on Linux and macOS, pip.ini on Windows.
| Level | Linux | macOS | Windows |
|---|---|---|---|
| Global | `/etc/pip.conf` | `/Library/Application Support/pip/pip.conf` | `C:\ProgramData\pip\pip.ini` |
| User | `~/.config/pip/pip.conf` | `~/Library/Application Support/pip/pip.conf` | `%APPDATA%\pip\pip.ini` |
| Site | `$VIRTUAL_ENV/pip.conf` | `$VIRTUAL_ENV/pip.conf` | `%VIRTUAL_ENV%\pip.ini` |
The site file lives at the root of the active virtual environment. That makes it a good place for settings that belong to one project. It also disappears when you delete the environment.
Do not rely on this table from memory. Ask pip which files it checks and which it actually loaded:
python -m pip config debugThe output lists each candidate path and marks the ones that exist, with their contents. This is the first command to run when a setting seems to be ignored.
File format
A config file is an INI file. A [global] section applies to every pip command. A section named after a command, such as [install], applies only to that command.
[global]
require-virtualenv = true
timeout = 30
[install]
index-url = https://pypi.org/simple
retries = 3With this file, pip install refuses to run outside a virtual environment. The require-virtualenv option is a cheap safeguard against the "wrong interpreter" problem from the troubleshooting chapter. Values for flag-style options are written as true or false.
Reading and writing configuration with pip config
You can edit files by hand, but pip config handles paths for you:
python -m pip config list
python -m pip config set global.timeout 30
python -m pip config set --site global.require-virtualenv true
python -m pip config get global.timeout
python -m pip config unset global.timeoutThe key is written as section.option. With no level flag, set writes to the user file. Use --user, --global, or --site to choose another level. --site needs an active virtual environment. pip config list shows the merged result of every source, so it answers "what will pip actually use?"
To run one command while ignoring all configuration files and PIP_ variables, add --isolated. This is useful for checking whether a config setting is causing a failure.
Choosing a different package index
An index is a web service that pip queries for the list of files available for a project name. pip expects the "simple" repository format that PyPI uses. Private servers such as devpi, Artifactory, Nexus, and cloud-hosted registries all provide it.
There are two options for choosing indexes, and they behave very differently.
--index-url(or-i) replaces PyPI. pip looks only at this index.--extra-index-urladds another index. pip looks at PyPI and the extra one, then picks the best matching version across all of them.
python -m pip install --index-url https://pypi.example.com/simple requests
python -m pip install --extra-index-url https://internal.example.com/simple acme-utilsInstalling from files instead of an index
--find-links (or -f) points pip at a directory or a web page that lists package files directly, such as wheels and source archives. Combine it with --no-index to stop pip from contacting any index at all. That gives you an offline installation workflow:
# On a machine with internet access
python -m pip download -r requirements.txt -d wheelhouse
# On the offline machine, after copying the wheelhouse folder
python -m pip install --no-index --find-links wheelhouse -r requirements.txtpip download fetches the packages and their dependencies without installing them. The second command installs only from the folder and fails with "no matching distribution" if a file is missing. That failure is the point: nothing can be fetched from an unexpected place.
A wheelhouse built on one platform only works on machines with a matching operating system, architecture, and Python version. Build it on a machine that resembles the target.
Putting index options in a requirements file
A requirements file can carry its own pip options, one per line:
--index-url https://pypi.example.com/simple
requests>=2.31,<3This keeps the index choice next to the dependency list, so anyone running pip install -r requirements.txt gets the same source. The trade-off is that the file now contains infrastructure details. If it is shared publicly, internal hostnames leak.
Credentials
Never commit a username and password inside an index URL. A URL such as https://user:token@host/simple ends up in shell history, process listings, and logs. Safer options are:
- A
~/.netrcfile (_netrcon Windows) with restrictive file permissions. pip reads it for matching hosts. - The
keyringpackage. If it is installed in the environment, pip can fetch credentials from your system keychain. - A token injected through an environment variable by your CI system, with the config file referencing nothing secret.
Proxies, certificates, and trusted hosts
Three more options come up in corporate networks:
--proxy http://proxy.example.com:3128sends traffic through a proxy. pip also respectsHTTP_PROXYandHTTPS_PROXY.--cert /path/to/ca-bundle.pemtells pip to trust a custom CA bundle, which is needed when a company inspects TLS traffic.--trusted-host host.example.comturns off certificate checks for that host.
Prefer --cert over --trusted-host. Trusting a host removes the protection that HTTPS gives you, so a network attacker could serve altered packages. Use it only for a short test on a network you control.
The cache
pip keeps downloaded files and built wheels in a cache, so a second install of the same version is fast. Inspect and manage it with:
python -m pip cache dir
python -m pip cache info
python -m pip cache purgeIf a package seems stale or corrupted, --no-cache-dir skips the cache for one command. Purging is safe: pip simply downloads again.
A worked example
Suppose a team wants every developer to use an internal mirror and to be blocked from installing outside a virtual environment. A user config file does both:
[global]
require-virtualenv = true
index-url = https://pypi.example.com/simple
cert = /etc/ssl/certs/company-ca.pemSave it to the user path for your platform, then verify the result:
python -m pip config list
python -m pip config debugIf a developer needs to bypass the mirror once, a command-line option overrides the file:
python -m pip install --index-url https://pypi.org/simple requestsCommon errors
- A setting is ignored. Run
pip config debugto see which files exist. The file may be at a path pip does not check, or aPIP_variable may override it. - The option is in the wrong section. An option under
[install]has no effect onpip download. Put shared options under[global]. - Index options behave differently than expected. Check whether you used
--extra-index-urlwhere you wanted--index-url. - "Could not find a version that satisfies the requirement" with
--no-index. The wheelhouse lacks that file or has the wrong platform tags. - TLS errors behind a corporate proxy. Supply the company CA with
--certinstead of disabling verification.
Exercise
A project's pip.conf in the active virtual environment sets index-url = https://mirror.example.com/simple. Your shell has PIP_INDEX_URL=https://other.example.com/simple exported. You run python -m pip install --index-url https://pypi.org/simple requests. Which index does pip use, and which command would show you the configuration it merged from files and environment?
Show answer
pip uses https://pypi.org/simple, because command-line options override environment variables, and environment variables override configuration files. python -m pip config list shows the merged configuration (python -m pip config debug also lists which files were loaded).
Exercise
Why is --extra-index-url a poor choice for installing an internal package named acme-utils, and what is a safer setup?
Show answer
pip picks the highest version of acme-utils found on any configured index. If someone publishes a package with that name and a higher version on PyPI, pip installs it (dependency confusion). A safer setup uses a single --index-url pointing at a private index you control, which can proxy PyPI for public packages.