杰瑞科技汇

Python pip镜像源如何配置?

Of course! Using a Python pip mirror is a common and highly recommended practice, especially in China, but also for improving download speeds and reliability globally.

Python pip镜像源如何配置?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide covering everything from the basics to advanced configurations.

Why Use a Pip Mirror?

  1. Speed: Downloading packages from a server geographically closer to you is significantly faster than connecting to the default PyPI server.
  2. Reliability: The default PyPI server can sometimes be slow or temporarily unavailable. A mirror provides a reliable fallback.
  3. Firewall/Network Restrictions: In some corporate or institutional networks, direct access to PyPI might be blocked, while access to a specific mirror is allowed.

Commonly Used Mirrors

Here are some of the most popular and reliable mirrors:

Mirror Name Location URL Notes
豆瓣 (Douban) China https://pypi.douban.com/simple/ One of the most popular and stable mirrors in China.
清华大学 (Tsinghua) China https://pypi.tuna.tsinghua.edu.cn/simple/ Another excellent and fast mirror from a top Chinese university.
阿里云 (Alibaba Cloud) China https://mirrors.aliyun.com/pypi/simple/ Provided by Alibaba, very reliable and fast.
中国科学技术大学 (USTC) China https://pypi.mirrors.ustc.edu.cn/simple/ A long-standing and trusted mirror.
华为云 (Huawei Cloud) China https://repo.huaweicloud.com/repository/pypi/simple/ A reliable option from Huawei.
PyPI Official Global https://pypi.org/simple/ The default, always good to have as a fallback.

How to Use a Mirror (Methods)

There are three primary ways to configure pip to use a mirror.

Method 1: Temporary Usage (Recommended for Testing)

You can specify the mirror URL directly in the pip install command. This is useful for a one-time installation or to test a specific mirror.

Python pip镜像源如何配置?-图2
(图片来源网络,侵删)
# Install a package using the Douban mirror
pip install -i https://pypi.douban.com/simple/ requests
# Install a package with version pinning using the Tsinghua mirror
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ "numpy==1.21.0"

Key Flags:

  • -i or --index-url: Specifies the URL of the package index (the mirror).

Method 2: Permanent Configuration for the Current User (Most Common)

This method sets the mirror for your user account only, without requiring sudo and affecting other users on the system.

  1. Create or Edit the Configuration File: The file is located at ~/.pip/pip.conf on Linux and macOS, and %APPDATA%\pip\pip.ini on Windows.

  2. Add the Mirror Configuration: Open the file (create it if it doesn't exist) and add the following content.

    [global]
    index-url = https://pypi.douban.com/simple/
    # Optional: Add trusted hosts to avoid SSL certificate warnings
    [install]
    trusted-host = pypi.douban.com

    Explanation:

    • [global] index-url: This tells pip to always use this URL as its default package source.
    • [install] trusted-host: Some mirrors might not have a valid SSL certificate for all their subdomains. Adding this line tells pip not to verify the host's SSL certificate, preventing SSL: CERTIFICATE_VERIFY_FAILED errors. Only do this for mirrors you trust.

Now, any pip install command you run will automatically use the configured mirror.

Method 3: System-wide Configuration (Requires sudo)

This method sets the mirror for all users on the system. You will likely need administrator privileges.

  1. Create or Edit the Configuration File: The file is located at /etc/pip.conf on Linux and macOS.

  2. Add the Mirror Configuration: The content is the same as the user-level configuration.

    [global]
    index-url = https://mirrors.aliyun.com/pypi/simple/
    [install]
    trusted-host = mirrors.aliyun.com

Advanced Configuration: Using Multiple Mirrors with extra-index-url

Sometimes, a mirror might not have the very latest packages or a specific package. You can configure pip to try multiple sources in order using the extra-index-url option.

In your pip.conf file, you can list the primary mirror first, followed by a fallback.

[global]
index-url = https://pypi.douban.com/simple/
# Try the Douban mirror first, then fall back to the official PyPI
extra-index-url = https://pypi.org/simple/
[install]
trusted-host = pypi.douban.com

With this setup:

  1. pip will first try to download packages from https://pypi.douban.com/simple/.
  2. If a package is not found there, it will automatically try to find it on the official https://pypi.org/simple/.

This gives you the speed of a mirror with the completeness of the official PyPI.


How to Revert to the Default PyPI

If you want to stop using a mirror and go back to the official PyPI, simply edit your pip.conf file and either delete the index-url line or comment it out by putting a at the beginning of the line.

# [global]
# index-url = https://pypi.douban.com/simple/
[install]
# trusted-host = pypi.douban.com

After saving the file, your next pip install command will use the default https://pypi.org/simple/ URL.

Summary Table

Method Command/File When to Use
Temporary pip install -i <mirror_url> <package> Quick, one-time installs or testing.
User-Level ~/.pip/pip.conf Recommended. For personal use, no sudo needed.
System-Level /etc/pip.conf For all users on a machine, requires sudo.
Multiple Mirrors extra-index-url in pip.conf For a robust setup with a fast primary and a reliable fallback.
分享:
扫描分享到社交APP
上一篇
下一篇