1. Why Is Pip So Slow? The Root Cause Lies in Server Location
For Python learners and developers, `pip` is an indispensable tool for managing third-party packages, and it is used almost every day. However, many people have encountered the same pain point—`pip` is extremely slow when installing packages. Especially when installing packages with many dependencies such as `PySide6`, the progress bar stays stuck for a long time. Even with sufficient network bandwidth, you can only wait helplessly.
In fact, the core reason for slow `pip` downloads is very simple: `pip` connects to Python package servers abroad by default. When domestic users access these servers, they are affected by network latency, cross-ocean link congestion and other issues, resulting in significantly reduced download speeds. Many people mistakenly think that there is a problem with their own network, try restarting the router or switching networks, but with little effect. In fact, as long as you find the right method, you can easily solve this problem and achieve a qualitative leap in `pip` installation speed.
2. Temporary Speed-Up Solution: Specify Domestic Mirror Sources When Installing Packages
If you only need to quickly install a Python package occasionally and do not want to perform complex settings, then temporarily specifying a domestic mirror source is the most convenient solution. There are many high-quality `pip` mirror sources in China, such as Alibaba Cloud Mirror, Tsinghua Mirror, Tencent Mirror, etc. These mirror sources synchronize the content of official packages and have servers in China, so the access speed is very fast.
The specific operation steps are as follows:
- Open the terminal or command prompt (take VS Code’s terminal as an example, click `View`->`Terminal` in the top menu bar to open it).
- If a slow `pip` installation is in progress, press the `Ctrl + C` key combination to stop the installation process first.
- Add the `-i` parameter after the installation command, followed by the address of the domestic mirror source. The format is: `pip install package-name -i mirror-source-address`
- Taking the installation of `PySide6` as an example, the commands for using different mirror sources are as follows:
– Alibaba Cloud Mirror: `pip install PySide6 -i https://mirrors.aliyun.com/pypi/simple/`
– Tsinghua Mirror: `pip install PySide6 -i https://pypi.tuna.tsinghua.edu.cn/simple/`
– Tencent Mirror: `pip install PySide6 -i https://mirrors.cloud.tencent.com/pypi/simple/` - Enter the command and press Enter to execute it. You can clearly see that the download speed is greatly improved. The installation process that originally took several minutes may now only take tens of seconds.
The advantage of this method is that it is flexible and fast, and does not require modifying any system settings. The disadvantage is that you need to manually add the mirror source parameter every time you install a package, which is suitable for temporary use scenarios.
3. Permanent Speed-Up Solution: Configure Global Pip Mirror Sources
If you need to use `pip` to install packages frequently, manually adding mirror source parameters every time will be very troublesome. In this case, you can choose to configure global pip mirror sources, set it once and benefit for a lifetime. Every time you use the `pip install` command in the future, it will automatically download packages from domestic mirror sources without adding additional parameters.
Taking the Windows system as an example, the specific operation steps are as follows:
- Open File Explorer. You can quickly open it by clicking the folder icon on the taskbar or pressing the `Win + E` key combination.
- Enter `%APPDATA%` in the address bar of File Explorer, then press Enter to enter the application data directory of the system.
- In this directory, create a folder named `pip`. If the folder already exists, skip this step directly.
– Right-click on the blank space, select `New`->`Folder`, and name the folder `pip`. - Enter the `pip` folder and create a text file named `pip.ini` in it.
– Right-click on the blank space, select `New`->`Text Document`, and rename the file to `pip.ini` (note that the extension is `.ini`, not `.txt`). - Right-click the `pip.ini` file, select `Open with`, and open the file with Notepad or other text editors.
- Enter the following content in the file (taking Alibaba Cloud Mirror as an example, you can replace it with other mirror sources according to your needs):
[global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host = mirrors.aliyun.com
- Save the file and close the editor.
- Verify whether the setting takes effect: Open the terminal, enter the command `pip config list`. If the output result contains `global.index-url = https://mirrors.aliyun.com/pypi/simple/`, it means the configuration is successful.
After the configuration is completed, when you use the `pip install package-name` command in the future, it will automatically download packages from the Alibaba Cloud Mirror source at a fast speed without manually adding parameters.
4. Conclusion: Choose Two Solutions According to Your Needs and Say Goodbye to Pip Lag Once and for All
Whether it is temporary speed-up or permanent speed-up, the core idea is to switch the download source of pip to domestic mirror servers. Both solutions have their own advantages and disadvantages, and you can choose flexibly according to your own usage scenarios:
- Choose Solution 2 for temporary use, which is fast and convenient without modifying system settings;
- Choose Solution 3 for frequent use, which can be configured once and for all.
Mastering these two methods, you no longer have to endure the slow speed of `pip` when installing packages, which can save a lot of time and greatly improve the efficiency of Python development and learning.
5. Demo Video
You can watch the following demo video by select the subtitle to your preferred subtitle language.