pip和conda使用其他python包安装源

本文详细介绍了如何在Linux和Windows系统下配置pip和conda的镜像源,包括临时和永久配置的方法,以及如何通过修改配置文件来指定源地址,帮助用户提高下载速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. pip 使用其他源

1.1 linux系统

  1. 临时使用清华源更新tensorflow
pip install --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu
  1. 永久配置 pip源
  • 找到 ~/.pip/pip.conf 文件,没有的话新建该文件
  • ~/.pip/pip.conf 文件中添加
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

2 windows系统

  临时使用的和linux系统操作一样,如果需要永久配置,操作如下。

  • 进入/c/Users/All Users/目录
  • 新建pip文件夹,并在文件夹中添加pip.ini文件
  • 在文件中加入以下内容
    [global]
    index-url = http://mirrors.aliyun.com/pypi/simple/
    [install]
    trusted-host = mirrors.aliyun.com

2. conda 添加其他源

  1. 命令行配置
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
# 以上两条是Anaconda官方库的镜像

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
# 以上是Anaconda第三方库 Conda Forge的镜像

# for linux
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
# for legacy win-64
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/peterjc123/
以上两条是Pytorch的Anaconda第三方镜像

conda config --set show_channel_urls yes

conda info 查看是否配置成功
3. 配置文件修改
通过修改用户目录下的.condarc配置文件可以配置conda的源

channels:
  - defaults
show_channel_urls: true
default_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/pkgs/r
custom_channels:
  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: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

在没有.condarc文件的情况下运行生成该文件

conda config --set show_channel_urls yes
### 如何实现 pip conda管理共享 在实际开发过程中,尽管不推荐混用 `conda` `pip` 来安装 Python [^1],但在某些情况下确实需要两者协作完成环境配置。以下是关于如何让 `pip` `conda` 安装能够共用的关键点: #### 1. **确保 pip 安装位于当前 conda 环境** 当在一个激活的 conda 环境中运行 `pip install` 命令时,默认会将安装到该环境中。然而,在一些特殊场景下(例如未正确激活环境),可能会导致被错误地安装到全局路径或其他地方[^3]。 为了避免这种情况发生,请始终确认已激活目标 conda 环境外再执行 `pip` 操作: ```bash conda activate your_env_name pip install package_name ``` 如果发现之前通过 `pip` 安装的部分并未进入指定 conda 环境,则可以尝试重新定位这些并将其移入对应目录;或者删除后再按照上述方法重装。 #### 2. **同步依赖关系表** 为了维护一致性可移植性,强烈建议同时保存两份描述文件——即用于记录 conda 所有组件及其版本号的 `environment.yml` 文件以及专属于 pip 的需求清单 `requirements.txt`[^2]。这有助于其他开发者依据个人偏好选用工具重建完全相同的项目工作区。 - 创建或更新 YAML 格式的定义文档: ```yaml name: my_project_env channels: - defaults dependencies: - numpy=1.x.y - pandas>=0.z.a - matplotlib==b.c.d - pip: - some_pure_python_package==e.f.g ``` - 导出纯由 pip 处理的内容至 TXT 文档形式: ```bash pip freeze > requirements.txt ``` 之后分享给同事时只需附带这两类元数据即可轻松重现整个生态系统。 #### 3. **优化网络资获取效率** 鉴于国内外访问速度差异较大可能引起下载延迟甚至失败等问题,合理利用本地镜像站点能有效提升构建过程中的流畅度。对于 conda 用户而言,可通过修改 `.condarc` 配置来设定首选服务器地址列表; 而对于 pip 使用者来说则需编辑用户家目录下的 `~/.pip/pip.conf` 或 Windows 下 `%APPDATA%\pip\pip.ini` 添加如下字段: ```ini [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple/ timeout = 60 ``` 以上措施均旨在促进跨平台间的无缝衔接体验的同时保障长期稳定性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值