找回密码
 立即注册
首页 业界区 业界 关于python更换永久镜像源

关于python更换永久镜像源

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

方法一:使用命令直接配置(推荐)
  1. # 清华镜像源
  2. pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  3. # 或者使用阿里云镜像源
  4. pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
  5. # 或者使用中科大镜像源
  6. pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/
复制代码
方法二:手动创建配置文件

Windows系统:
  1. # 在用户目录下创建pip文件夹和pip.ini文件
  2. # 路径:%USERPROFILE%\pip\pip.ini
复制代码
Linux/Mac系统:
  1. # 在用户目录下创建.pip文件夹和pip.conf文件
  2. # 路径:~/.pip/pip.conf
复制代码
配置文件内容:
  1. [global]
  2. index-url = https://pypi.tuna.tsinghua.edu.cn/simple
  3. trusted-host = pypi.tuna.tsinghua.edu.cn
  4. timeout = 6000
  5. [install]
  6. trusted-host = pypi.tuna.tsinghua.edu.cn
复制代码
2. conda镜像源配置

方法一:使用命令配置
  1. # 添加清华镜像源
  2. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  3. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  4. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  5. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  6. # 设置搜索时显示通道地址
  7. conda config --set show_channel_urls yes
  8. # 移除默认源(可选)
  9. conda config --remove channels defaults
  10. # 查看当前配置
  11. conda config --show
复制代码
方法二:直接编辑.condarc文件

在用户目录下创建或编辑.condarc文件:
Windows: C:\Users\用户名\.condarc
Linux/Mac: ~/.condarc
  1. channels:
  2.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  3.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  4.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  5.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  6.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  7.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
  8. show_channel_urls: true
  9. 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. 验证配置

配置完成后,可以通过以下命令验证:
  1. # 查看pip配置
  2. pip config list
  3. # 查看conda配置
  4. conda config --show
  5. # 测试安装包(pip)
  6. pip install requests -v
  7. # 测试安装包(conda)
  8. conda install numpy -v
复制代码
5. 恢复默认源

如果需要恢复默认源:
  1. # pip恢复默认
  2. pip config unset global.index-url
  3. # conda恢复默认
  4. conda config --remove-key channels
复制代码
选择离你地理位置较近的镜像源可以获得更好的下载速度。清华镜像源在国内访问速度通常比较稳定。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册