pip 指定国内镜像,并设置不使用 https
在使用 pip
安装 Python 包时,指定国内镜像可以显著加快下载速度,因为默认的 PyPI 源(https://pypi.org/)在国内访问可能较慢。同时,如果你希望避免使用 HTTPS(例如在某些特殊网络环境下),可以配置 HTTP 镜像源。以下是详细步骤和说明。
一、国内常用镜像源
以下是一些常用的国内 PyPI 镜像源(HTTP 协议):
- 阿里云:
http://mirrors.aliyun.com/pypi/simple/
- 清华大学:
http://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
- 中国科学技术大学:
http://mirrors.ustc.edu.cn/pypi/web/simple/
- 豆瓣:
http://pypi.doubanio.com/simple/
注意:大多数镜像源默认使用 HTTPS,但部分镜像仍支持 HTTP。如果你需要强制使用 HTTP,请确保镜像源提供 HTTP 地址,否则会报错。
二、临时指定国内镜像(单次使用)
在命令行中通过 -i
参数指定镜像源,并添加 --trusted-host
参数以避免 HTTPS 验证警告。
示例:使用阿里云 HTTP 镜像安装 tqdm
pip install tqdm -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
-i
:指定镜像源地址。--trusted-host
:绕过 HTTPS 验证,防止提示 “not a trusted host”。
说明:
- 如果镜像源只提供 HTTPS(如清华大学的
https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
),则无法直接使用 HTTP 协议。此时需要找到支持 HTTP 的镜像或调整网络环境。
三、永久配置国内镜像
为了避免每次都手动指定镜像,可以修改 pip
的配置文件,永久设置为国内 HTTP 镜像。
1. 找到配置文件
- Linux/macOS:
~/.pip/pip.conf
- Windows:
%USERPROFILE%\pip\pip.ini
如果文件不存在,手动创建:
- Linux/macOS:
mkdir ~/.pip
touch ~/.pip/pip.conf
- Windows:
- 创建目录:
%USERPROFILE%\pip
(通常是C:\Users\<用户名>\pip
)。 - 创建文件
pip.ini
。
2. 编辑配置文件
编辑 pip.conf
或 pip.ini
,添加以下内容(以阿里云 HTTP 镜像为例):
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
index-url
:指定默认镜像源。trusted-host
:避免 HTTPS 验证警告。
3. 验证配置
运行以下命令检查是否生效:
pip config list
输出类似:
global.index-url='http://mirrors.aliyun.com/pypi/simple/'
global.trusted-host='mirrors.aliyun.com'
现在,运行 pip install <package>
会默认使用配置的 HTTP 镜像。
四、结合 ChromeDriver 和 REST API 示例
如果你正在安装与 ChromeDriver 或 REST API 相关的包(如 selenium
或 requests
),可以使用国内镜像加速。例如:
pip install selenium -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
结合 Cron 定时任务:
如果你需要定时运行 pip
安装或升级包(如维护 Selenium 项目),可以创建一个脚本并通过 Cron 执行:
#!/bin/bash
# 文件名:update_packages.sh
pip install --upgrade selenium -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
添加 Cron 任务(每天凌晨 3:00 执行):
crontab -e
添加:
0 3 * * * /bin/bash /path/to/update_packages.sh >> /var/log/pip_update.log 2>&1
结合进度条:
如果批量安装多个包,可以用 tqdm
显示进度(需先安装 tqdm
)。示例:
from tqdm import tqdm
import subprocess
packages = ['selenium', 'requests', 'tqdm']
for pkg in tqdm(packages, desc="Installing packages"):
subprocess.run(['pip', 'install', pkg, '-i', 'http://mirrors.aliyun.com/pypi/simple/', '--trusted-host', 'mirrors.aliyun.com'])
五、注意事项
- HTTP 安全风险:
- 使用 HTTP 镜像可能存在数据被拦截或篡改的风险,尤其是在不可信网络中。建议优先使用 HTTPS 镜像,除非网络环境受限。
- 如果镜像源不支持 HTTP,尝试更换其他镜像(如豆瓣)或检查网络代理设置。
- 镜像同步延迟:
- 国内镜像可能与 PyPI 主站存在同步延迟,新发布的包可能暂时不可用。此时可临时切换回默认源:
bash pip install <package> -i https://pypi.org/simple
- 调试输出:
- 使用
--verbose
查看详细安装日志,类似 JavaScript 的console.log()
:bash pip install tqdm --verbose -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
- Windows 特殊字符问题:
- 在 Windows 的
pip.ini
中,确保使用正确的文件编码(UTF-8)并避免 BOM 问题。 - 如果路径有中文,建议将
pip.ini
放在默认位置(%USERPROFILE%\pip
)。
- 代理环境:
- 如果网络环境要求代理,可以设置 HTTP 代理:
bash export http_proxy="http://proxy.example.com:8080" pip install <package> -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
六、总结
- 通过
-i
和--trusted-host
参数,pip
可以轻松使用国内 HTTP 镜像加速下载。 - 永久配置通过修改
pip.conf
或pip.ini
实现,适合长期使用。 - 结合 ChromeDriver、REST API 或 Cron 任务时,国内镜像可显著提高效率。
- 优先考虑 HTTPS 镜像以确保安全,HTTP 仅在必要时使用。
如果需要特定包的安装示例、调试帮助,或结合 Selenium/REST API 的更复杂场景,请提供更多细节!