关于python更换永久镜像源

在Python中更换永久镜像源主要有两种方式:pip镜像源和conda镜像源。以下是详细的配置方法:

1. pip镜像源配置

方法一:使用命令直接配置(推荐)

bash 复制代码
# 清华镜像源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 或者使用阿里云镜像源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 或者使用中科大镜像源
pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/

方法二:手动创建配置文件

Windows系统:

bash 复制代码
# 在用户目录下创建pip文件夹和pip.ini文件
# 路径:%USERPROFILE%\pip\pip.ini

Linux/Mac系统:

bash 复制代码
# 在用户目录下创建.pip文件夹和pip.conf文件
# 路径:~/.pip/pip.conf

配置文件内容:

ini 复制代码
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
timeout = 6000

[install]
trusted-host = pypi.tuna.tsinghua.edu.cn

2. conda镜像源配置

方法一:使用命令配置

bash 复制代码
# 添加清华镜像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/

# 设置搜索时显示通道地址
conda config --set show_channel_urls yes

# 移除默认源(可选)
conda config --remove channels defaults

# 查看当前配置
conda config --show

方法二:直接编辑.condarc文件

在用户目录下创建或编辑.condarc文件:

Windows: C:\Users\用户名\.condarc
Linux/Mac: ~/.condarc

yaml 复制代码
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
show_channel_urls: true
ssl_verify: true

3. 常用镜像源地址

pip镜像源:

  • 清华https://pypi.tuna.tsinghua.edu.cn/simple
  • 阿里云https://mirrors.aliyun.com/pypi/simple/
  • 中科大https://pypi.mirrors.ustc.edu.cn/simple/
  • 豆瓣https://pypi.douban.com/simple/
  • 华为云https://repo.huaweicloud.com/repository/pypi/simple/

conda镜像源:

  • 清华https://mirrors.tuna.tsinghua.edu.cn/anaconda/
  • 中科大https://mirrors.ustc.edu.cn/anaconda/

4. 验证配置

配置完成后,可以通过以下命令验证:

bash 复制代码
# 查看pip配置
pip config list

# 查看conda配置
conda config --show

# 测试安装包(pip)
pip install requests -v

# 测试安装包(conda)
conda install numpy -v

5. 恢复默认源

如果需要恢复默认源:

bash 复制代码
# pip恢复默认
pip config unset global.index-url

# conda恢复默认
conda config --remove-key channels

选择离你地理位置较近的镜像源可以获得更好的下载速度。清华镜像源在国内访问速度通常比较稳定。

相关推荐
vortex519 小时前
python 库劫持:原理、利用与防御
python·网络安全·提权
捉鸭子20 小时前
某音a_bogus vmp逆向
爬虫·python·web安全·node.js·js
曲幽20 小时前
FastAPI 生产环境静态文件完全指南:从 /favicon.ico 404 到 HSTS 混合内容,一次全根治
python·fastapi·web·static·media·404·hsts·favicon·url_for
Dontla20 小时前
Python asyncpg库介绍(基于Python asyncio的PostgreSQL数据库驱动)连接池、SQLAlchemy
数据库·python·postgresql
zh15702320 小时前
如何编写动态SQL存储过程_使用sp_executesql执行灵活查询
jvm·数据库·python
2401_8242226920 小时前
SQL报表统计数据量巨大_分批统计策略
jvm·数据库·python
X566120 小时前
mysql如何处理连接数过多报错_调整max_connections参数
jvm·数据库·python
m0_6091604920 小时前
MongoDB中什么是Hashed Shard Key的哈希冲突_哈希函数的分布均匀性分析
jvm·数据库·python
Ulyanov20 小时前
《现代 Python 桌面应用架构实战:PySide6 + QML 从入门到工程化》 开发环境搭建与工具链极简主义 —— 拒绝臃肿,构建工业级基座
开发语言·python·qt·ui·架构·系统仿真
wuxinyan12320 小时前
大模型学习之路03:提示工程从入门到精通(第三篇)
人工智能·python·学习