Problem
The default pip install package-name command pulls packages directly from the official PyPI repository at https://pypi.python.org/pypi. For users in certain regions, download speeds can be extremely slow. While you can specify an alternative mirror with the -i flag during installation, configuring a persistent default source eliminates the need for repetitive command-line options.
Solution 1: Using the psm Tool
psm (PyPI Source Manager) is a command-line utility that simplifies switching between package sources. The source code is available on GitHub.
Installation
pip install psm
For systems with both Python 2 and Python 3, install for Python 3 explicitly:
pip3 install psm
Available Commands
List all configured mirrors:
psm ls
The tool supports the following mirrors:
| Name | URL |
|---|---|
| pypi | https://pypi.python.org/simple/ |
| douban | https://pypi.douban.com/simple/ |
| aliyun | http://mirrors.aliyun.com/pypi/simple/ |
Display the currently active source:
psm show
# Output: Current source is douban
Switch to a different mirror:
psm use douban
# Output: Source is changed to douban
Usage on Windows
Install the package:
pip install psm
Execute commands using the Python module syntax:
python -m psm ls
python -m psm use aliyun
python -m psm show
Solution 2: Configuration File
Available Mirrors
Several Chinese institutions provide pip mirrors:
-阿里云: http://mirrors.aliyun.com/pypi/simple/
- 中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/
- 豆瓣: https://pypi.douban.com/simple/
- 清华大学: https://pypi.tuna.tsinghua.edu.cn/simple/
Temporary Usage
To use a mirror for a single installation without modifying configuration files:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple package-name
Linux Configuration
For pip version 10.0.0 and above, use the config command:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
To upgrade pip itself through a mirror when network connectivity is poor:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
Alternatively, manually edit ~/.config/pip/pip.conf (create the file if it does not exist):
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
The trusted-host option is required for HTTP sources or when using HTTPS with custom certificates.
Windows Configuration
- Open File Explorer and navigate to
%APPDATA% - Create a
pipfolder if it does not exist - Create
pip.iniinside this folder - Add the following content:
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
The full path becomes: %APPDATA%\pip\pip.ini
macOS Configuration
Create the configuration file at:
~/Library/Application Support/pip/pip.conf
Or alternatively at:
~/.config/pip/pip.conf
Add the following content:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
Important Notes
Configuration file modifications may occasionally cause errors. Prefer HTTPS sources over plain HTTP connections for improved security and reliability. Note that mirror synchronization delays vary—some packages like TensorFlow may not reflect the latest versions immediately on third-party mirrors.