Python镜像源配置及常见问题解决
常用国内镜像源列表
以下是一些常用的国内Python镜像源:
- 阿里云:
https://mirrors.aliyun.com/pypi/simple/
- 清华大学:
https://pypi.tuna.tsinghua.edu.cn/simple/
- 豆瓣:
http://pypi.douban.com/simple/
- 中国科学技术大学:
https://pypi.mirrors.ustc.edu.cn/simple/
- 华为云:
https://repo.huaweicloud.com/repository/pypi/simple/
临时使用镜像源
在安装包时使用-i
参数指定镜像源:
pip install 包名 -i https://mirrors.aliyun.com/pypi/simple/
永久配置镜像源
Windows系统
- 在用户目录下创建
pip
文件夹(如C:\Users\用户名\pip
) - 在
pip
文件夹中创建pip.ini
文件,内容如下:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
Linux/macOS系统
- 在用户目录下创建
.pip
文件夹(~/.pip
) - 在
.pip
文件夹中创建pip.conf
文件,内容如下:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
常见问题及解决方法
1. SSL证书错误
错误信息:
Could not fetch URL https://pypi.org/simple/: There was a problem confirming the ssl certificate
解决方法:
- 添加
--trusted-host
参数:pip install 包名 --trusted-host mirrors.aliyun.com -i https://mirrors.aliyun.com/pypi/simple/
- 或者在配置文件中添加
trusted-host
(如上文永久配置所示)
2. 镜像源不可用
错误信息:
Could not fetch URL https://mirrors.xxx.com/pypi/simple/: connection error
解决方法:
- 尝试更换其他镜像源
- 检查网络连接是否正常
3. 版本冲突
错误信息:
ERROR: Cannot install 包名 because these package versions have conflicting dependencies.
解决方法:
- 使用
--upgrade
参数升级相关包:pip install --upgrade 包名
- 或者指定版本安装:
pip install 包名==版本号
4. 权限问题
错误信息:
PermissionError: [Errno 13] Permission denied
解决方法:
- 使用
--user
参数安装到用户目录:pip install --user 包名
- 或者使用虚拟环境
5. 超时问题
错误信息:
ReadTimeoutError: HTTPSConnectionPool(host='mirrors.xxx.com', port=443): Read timed out.
解决方法:
- 增加超时时间:
pip --default-timeout=100 install 包名
- 或者更换网络环境
其他实用命令
-
查看当前配置:
pip config list
-
清除缓存:
pip cache purge
-
查看已安装包:
pip list
-
导出已安装包列表:
pip freeze > requirements.txt
-
从文件安装:
pip install -r requirements.txt
希望这些信息能帮助您更好地配置和使用Python镜像源!