安装和使用conda

Conda 是一个开源的软件包管理系统和环境管理系统,用于安装多个版本的软件包及其依赖关系,并在它们之间轻松切换。可以创建多个环境,并在环境中使用不同的python版本,并安装环境专属的python依赖包,可以用来避免python项目依赖冲突的问题。

conda用户引导

https://conda.io/projects/conda/en/latest/user-guide/getting-started.html

安装

Anaconda-Navigator 下载地址

根据个人的操作系统环境下载对应的版本,然后本地安装即可。

安装完成后会出现Anaconda-Navigator这个应用,并且在终端中就可以使用conda命令了

配置conda镜像源

由于使用conda安装第三方包的时候,默认是国外的镜像很容易出现安装失败,所以需要添加国内的镜像源,这里推荐配置清华的镜像源。

使用终端输入以下命令

bash 复制代码
echo "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/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
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
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud" > ~/.condarc

清除索引缓存,保证用的是镜像站提供的索引

bash 复制代码
conda clean -i

查看配置,输入下面的命令,出现了https://mirrors.tuna.tsinghua.edu.cn的关键字,就说明配置成功了

bash 复制代码
conda config --show

配置pip的安装源

很多项目一般使用pip去依赖的,不切换镜像源同样会有安装依赖慢的问题,这里设置一下pip的安装源

bash 复制代码
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/

查看配置的安装源 pip config get global.index-url

bash 复制代码
pip config get global.index-url

输出https://pypi.tuna.tsinghua.edu.cn/simple/就表示配置成功了

Anaconda-Navigator使用

创建环境

输入环境名称,最好是英文,然后选择需要的python版本,点击创建即可

打开终端并进入某个环境

包搜索

https://anaconda.org/

命令行操作

大部分功能直接使用Anaconda-Navigator也可以

创建环境
bash 复制代码
conda create -n <env-name>
# 创建并安装依赖
conda create -n <env-name> python numpy pandas
指定环境中的python版本
bash 复制代码
# 在创建环境时安装指定的python版本
conda create -n <env-name> python=3.11
# 切换到某个环境里然后更新python
conda install python=3.12
查看环境
bash 复制代码
conda info --envs
删除环境
bash 复制代码
conda env remove -n env_name
激活环境
bash 复制代码
conda activate 环境名称
安装依赖

建议进入某个环境后安装

bash 复制代码
conda install 包名
# 或者
pip install 包名
卸载第三方包
bash 复制代码
conda remove numpy
# 或者
pip uninstall numpy
# 删除所有
conda remove --all
查看安装的依赖
bash 复制代码
conda list
导入导出环境

导出当前环境的包信息可以用

bash 复制代码
conda env export > 具体路径
# eg:conda env export > C:\Users\pc\Desktop\environment.yaml

用配置文件(yaml)创建新的虚拟环境

bash 复制代码
conda env create -n 环境名 -f C:\Users\pc\Desktop\environment.yaml
更新conda
bash 复制代码
conda update conda
相关推荐
正在走向自律5 小时前
Conda 完全指南:从环境管理到工具集成
开发语言·python·conda·numpy·fastapi·pip·开发工具
小oo呆1 天前
【学习心得】Jupyter 如何在conda的base环境中其他虚拟环境内核
python·jupyter·conda
zhang-hui1 天前
PowerShell 实现 conda 懒加载
conda
请你喝好果汁6411 天前
学习笔记:Conda 环境共享
笔记·学习·conda
珊珊而川1 天前
conda init执行了还是不好用
conda
Eric.Lee20212 天前
Ubuntu shell指定conda的python环境启动脚本
python·ubuntu·conda·shell
Echo丶洛尘2 天前
Pycharm中No Conda enviroment selected
pycharm·conda
tyn1883 天前
记录一次conda虚拟环境pip安装报错[WinError 32] 另一个程序正在使用此文件,进程无法访问
windows·conda·pip·虚拟环境·虚环境
陈苏同学3 天前
[已解决] VS Code / Cursor / Trae 的 PowerShell 终端 conda activate 进不去环境的常见问题
linux·windows·conda
Eric.Lee20213 天前
conda 输出指定python环境的库 输出为 yaml文件
linux·python·conda