conda和pip换源指南

下面是一篇可以直接当教程用的文章,分两部分:

1)conda 查看/更换镜像源;2)pip 查看/更换镜像源。以 Ubuntu 为例,其实其他 Linux 也基本一样。


一、查看与更换 conda 源(镜像)

1. 查看当前 conda 源

打开终端,执行:

bash 复制代码
conda config --show channels

你会看到类似输出:

text 复制代码
channels:
  - defaults
show_channel_urls: True

常见情况:

  • 只有 defaults:说明用的是 Anaconda 官方默认源(在国内较慢)

  • 如果你之前设置过镜像,可能会看到类似:

    text 复制代码
    channels:
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/
      - defaults

也可以直接看配置文件:

bash 复制代码
cat ~/.condarc

如果文件不存在或是空的,就说明你几乎没做过 conda 的源配置。


2. 常用国内 conda 镜像(任选其一)

以常用的两个例子(地址可能会更新,可到官方镜像站核对):

  • 清华 TUNA:

    • 主仓库:
      • https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
      • https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
    • conda-forge:
      • https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  • 阿里云:

    • https://mirrors.aliyun.com/anaconda/pkgs/main
    • https://mirrors.aliyun.com/anaconda/pkgs/r

下面用清华举例,其他镜像替换 URL 即可。


3. 临时换源(只对当前命令生效)

如果你只是偶尔从镜像下载安装一个包,可以这样:

bash 复制代码
conda install numpy -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

或者使用 conda-forge 镜像:

bash 复制代码
conda install -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge package_name

这种方式不修改全局配置,适合临时用。


4. 永久更换 conda 源(写入 ~/.condarc)

4.1 备份原来的配置(推荐)

bash 复制代码
cp ~/.condarc ~/.condarc.bak 2>/dev/null || true

如果没有这个文件,会报错但被忽略(|| true),可以放心用。

4.2 设置为清华源示例

执行下面命令(会直接覆盖 .condarc 的 channels 部分):

bash 复制代码
conda config --remove-key channels 2>/dev/null || true
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/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --set show_channel_urls yes

然后再查看一下:

bash 复制代码
conda config --show channels
cat ~/.condarc

你应该能看到刚刚添加的镜像地址。

4.3 恢复官方默认源

如果以后想恢复到最原始的官方源:

bash 复制代码
mv ~/.condarc.bak ~/.condarc   # 如果你有之前备份
# 或者重置 channels:
conda config --remove-key channels

重置后,再执行:

bash 复制代码
conda config --show channels

一般会只看到:

text 复制代码
channels:
  - defaults

二、查看与更换 pip 源(镜像)

1. 查看当前 pip 源

1.1 查看用于 install 的当前 index-url

bash 复制代码
pip config get global.index-url 2>/dev/null || pip config get user.index-url 2>/dev/null || echo "未设置,使用官方默认源"

如果输出类似:

text 复制代码
https://pypi.tuna.tsinghua.edu.cn/simple

说明你正在用清华源;

如果没输出"http..."而是"未设置...",说明用的是官方 https://pypi.org/simple

1.2 直接从 config 文件看

Linux(Ubuntu) 下 pip 的配置文件路径可能是:

  • 系统级:/etc/pip.conf
  • 用户级:~/.pip/pip.conf~/.config/pip/pip.conf

可以逐个看:

bash 复制代码
cat /etc/pip.conf 2>/dev/null
cat ~/.pip/pip.conf 2>/dev/null
cat ~/.config/pip/pip.conf 2>/dev/null

如果其中有类似:

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

那就是你现在用的源。


2. 常用国内 pip 源(任选其一)

常见几家(以/simple 结尾):

  • 清华:https://pypi.tuna.tsinghua.edu.cn/simple
  • 阿里云:https://mirrors.aliyun.com/pypi/simple
  • 豆瓣:https://pypi.doubanio.com/simple
  • 中国科大:https://pypi.mirrors.ustc.edu.cn/simple

下面以清华为例。


3. 临时换源(只对这一次安装生效)

命令后面加 -i 参数即可:

bash 复制代码
pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple

这不会修改 pip 的全局配置。


4. 永久更换 pip 源(写入 pip.conf)

4.1 创建配置文件目录

Ubuntu 用户级通常用 ~/.pip/pip.conf

bash 复制代码
mkdir -p ~/.pip

4.2 写入清华源示例

bash 复制代码
cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
timeout = 60

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

然后检查:

bash 复制代码
cat ~/.pip/pip.conf
pip config get global.index-url 2>/dev/null || pip config get user.index-url

再次安装包时,如果输出里出现了清华的 URL,就说明换源成功,例如:

bash 复制代码
pip install requests

安装日志会显示从 pypi.tuna.tsinghua.edu.cn 下载。

如果你希望对全系统所有用户都生效,可以编辑 /etc/pip.conf,需要 sudo 权限:

bash 复制代码
sudo mkdir -p /etc
sudo nano /etc/pip.conf

内容同上。

4.3 恢复使用官方源

只要删除或注释掉 index-url 即可。

例如编辑 ~/.pip/pip.conf

bash 复制代码
nano ~/.pip/pip.conf

删除这一行:

ini 复制代码
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

或整份配置文件删掉:

bash 复制代码
rm ~/.pip/pip.conf

再检查:

bash 复制代码
pip config list

没有自定义 index-url 时,就会回到默认官方源 https://pypi.org/simple


三、conda 与 pip 共存时的建议

  1. 尽量用 conda 管理环境和大包(如 numpy、pytorch、tensorflow),涉及到 C/C++ 库时 conda 一般更省心。
  2. 在某个 conda 环境里再用 pip 补充 conda 里没有的 Python 包。
  3. 换源时:
    • conda 单独配置 .condarc
    • pip 单独配置 pip.conf
      两者互不干扰,可以都指向各自的镜像站。

如果你愿意,可以把下面这几条"检查命令"记下来,随时查看当前源:

bash 复制代码
# conda 当前源
conda config --show channels

# pip 当前源
pip config list
pip config get global.index-url 2>/dev/null || pip config get user.index-url 2>/dev/null
相关推荐
万粉变现经纪人1 天前
如何解决 pip install llama-cpp-python 报错 未安装 CMake/Ninja 或 CPU 不支持 AVX 问题
开发语言·python·开源·aigc·pip·ai写作·llama
Echo_NGC22373 天前
【论文解读】Attention Is All You Need —— AI 时代的“开山之作“,经典中的经典(transformer小白导读)
人工智能·python·深度学习·神经网络·机器学习·conda·transformer
何中应3 天前
Conda安装&使用
python·conda·python3.11
林浩杨4 天前
Pip对当前环境下的所有包进行更新
pip
qq_229058014 天前
conda中安装 rdkit版本的postgresql然后在Win11中使用虚拟环境里的rdkit
数据库·postgresql·conda
咯哦哦哦哦5 天前
Foundationpose环境配置【非conda--纯UV】(linux22.04+python3.10)
python·pip·uv
nashane6 天前
HarmonyOS 6学习:画中画(PiP)状态同步与场景化实战指南
学习·pip·harmonyos·harmonyos 5
Mr.朱鹏6 天前
【Python 进阶 | 第四篇】Psycopg3 + Flask 实现 PostgreSQL CRUD 全流程:从连接池到RESTful接口
python·postgresql·flask·virtualenv·fastapi·pip·tornado
独隅7 天前
Anaconda被误删后抢救手册
conda
矢志航天的阿洪7 天前
手动安装Gurobi并配置gurobipy到Python环境(Windows/Conda)
windows·python·conda