【Python】conda虚拟环境创建及使用

0.前言

conda 是一个开源的包、环境管理器,可以用于在同一个机器上创建不同的虚拟环境,安装不同 Python 版本的软件包及其依赖,并能够在不同的虚拟环境之间切换。Conda常通过安装Anaconda/Miniconda来进行使用。一般使用Miniconda就够了。

1.Miniconda安装

下载Miniconda安装包

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

安装Miniconda

bash Miniconda3-latest-Linux-x86_64.sh
...
Please answer 'yes' or 'no':'
>>> yes
...
Miniconda3 will now be installed into this location:
/root/miniconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/miniconda3] >>> 
...
You can undo this by running `conda init --reverse $SHELL`? [yes|no]
[no] >>> yes

配置国内镜像源

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/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
conda config --set show_channel_urls yes

到此,安装完成。

2.conda本地基本操作

获取版本号

conda --version 或 conda -V

检查更新当前conda

conda update conda

查看当前存在哪些虚拟环境

conda env list 或 conda info -e 或 conda info --envs

查看/安装/更新/删除包

conda list:
conda search package_name
conda install package_name
conda install package_name=1.5.0
conda update package_name
conda remove package_name

3.创建conda虚拟环境

创建名为your_env_name的环境

conda create --name your_env_name

创建制定python版本的环境

conda create --name your_env_name python=3.12

创建包含某些包的环境

conda create --name your_env_name numpy scipy

创建指定python版本下包含某些包的环境

conda create --name your_env_name python=3.12 numpy scipy

4.激活conda虚拟环境

Linux

 conda activate your_env_name

Windows

activate your_env_name

5.退出conda虚拟环境

Linux

conda deactivate

Windows

deactivate env_name

6.删除conda虚拟环境

conda remove -n your_env_name --all
conda remove --name your_env_name --all

7.克隆conda虚拟环境

conda create --name new_env_name --clone old_env_name
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸟哥大大

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值