conda管理Python库和虚拟环境

conda 是一个开源的跨平台软件包管理系统和环境管理系统,用于安装、运行和协调不同版本的软件包和其依赖项。它最初是为 Python 语言而设计的,但现在已经支持多种编程语言和工具。conda 可以轻松地创建和使用虚拟环境,这些环境可以独立于系统上的其他环境和安装的软件包运行。conda 还有一个广泛的软件包仓库,其中包含许多科学计算、数据分析和机器学习软件包,可供用户直接安装和使用。

conda 支持 Python、R、Java、JavaScript、C 等多种开发语言的包、依赖和环境管理工具,能运行在 Windows、MacOS、Linux 多个平台,可以在本地轻松创建、保存、切换环境。当安装 anaconda 时,会自动安装 conda 工具。

conda 与 pipenv,venv 等虚拟环境管理工具的最大的不同在于:conda 虚拟环境是独立于操作系统解释器环境的,即无论操作系统解释器什么版本(哪怕 2.7),我也可以指定虚拟环境 python 版本为 3.6,而 venv 是依赖主环境的。

查看conda版本

bash 复制代码
$conda --version
conda 23.1.0

查看帮助信息

bash 复制代码
$conda --help
usage: conda-script.py [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    clean             Remove unused packages and caches.
    compare           Compare packages between conda environments.
    config            Modify configuration values in .condarc. This is modeled
                      after the git config command. Writes to the user
                      .condarc file (C:\Users\user\.condarc) by default. Use
                      the --show-sources flag to display all identified
                      configuration locations on your computer.
    create            Create a new conda environment from a list of specified
                      packages.
    info              Display information about current conda install.
    init              Initialize conda for shell interaction.
    install           Installs a list of packages into a specified conda
                      environment.
    list              List installed packages in a conda environment.
    package           Low-level conda package utility. (EXPERIMENTAL)
    remove (uninstall)
                      Remove a list of packages from a specified conda
                      environment.
    rename            Renames an existing environment.
    run               Run an executable in a conda environment.
    search            Search for packages and display associated
                      information.The input is a MatchSpec, a query language
                      for conda packages. See examples below.
    update (upgrade)  Updates conda packages to the latest compatible version.
    notices           Retrieves latest channel notifications.

options:
  -h, --help          Show this help message and exit.
  -V, --version       Show the conda version number and exit.

查看安装包列表

bash 复制代码
$ conda list                                                     
# packages in environment at D:\anaconda3:                       
#                                                                
# Name                    Version                   Build  Channe
adbutils                  2.8.0                    pypi_0    pypi
aiohttp                   3.8.5                    pypi_0    pypi
aiosignal                 1.3.1                    pypi_0    pypi
aiosqlite                 0.20.0                   pypi_0    pypi
alabaster                 0.7.16                   pypi_0    pypi
albucore                  0.0.13                   pypi_0    pypi
albumentations            1.4.10                   pypi_0    pypi
altair                    5.2.0                    pypi_0    pypi
......

配置下载源

以清华源为例

bash 复制代码
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/

还有很多其他的镜像:

bash 复制代码
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/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/main/

查看配置结果:

bash 复制代码
$ conda config --show-sources                               
==> C:\Users\user\.condarc <==                             
ssl_verify: True                                            
channels:                                                   
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults                                                

配置显示下载源

bash 复制代码
conda config --set show_channel_urls yes

创建环境

bash 复制代码
conda create --name myenv python=3.8 requests

这里创建一个虚拟环境,名称是myenv,python版本是3.8,依赖库是requests。

激活环境

bash 复制代码
$ conda activate myenv

激活之后,命令行前面会显示(myenv)

关闭环境

bash 复制代码
$ conda deactivate

查看所有的环境

bash 复制代码
(myenv) λ conda env list                        
# conda environments:                            
#                                                
                         D:\Anaconda3            
base                     D:\anaconda3            
myenv                *  D:\anaconda3\envs\myenv

安装依赖包

bash 复制代码
(myenv) $conda install beautifulsoup4
Collecting package metadata (current_repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 23.1.0
  latest version: 24.11.3

Please update conda by running

    $ conda update -n base -c defaults conda

Or to minimize the number of packages updated during conda update use

     conda install conda=24.11.3



## Package Plan ##

  environment location: D:\anaconda3\envs\myenv

  added / updated specs:
    - beautifulsoup4


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    beautifulsoup4-4.12.3      |   py38haa95532_0         214 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    soupsieve-2.5              |   py38haa95532_0          70 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    ------------------------------------------------------------
                                           Total:         284 KB

The following NEW packages will be INSTALLED:

  beautifulsoup4     anaconda/pkgs/main/win-64::beautifulsoup4-4.12.3-py38haa95532_0
  soupsieve          anaconda/pkgs/main/win-64::soupsieve-2.5-py38haa95532_0


Proceed ([y]/n)? y


Downloading and Extracting Packages

Preparing transaction: done
Verifying transaction: done
Executing transaction: done

移除依赖包

bash 复制代码
conda remove beautifulsoup4

导出环境配置

bash 复制代码
conda env export > myenv.yml

删除环境

bash 复制代码
conda deactivate # 删除之前要先关闭当前环境
conda remove --name myenv --all

导入环境配置

bash 复制代码
conda env create -f myenv.yml

相关链接

https://blog.csdn.net/weixin_53287520/article/details/134704377

相关推荐
想回家的一天2 分钟前
ECONNREFUSED ::1:8000 前端代理问题
开发语言
cike_y4 分钟前
Mybatis之解析配置优化
java·开发语言·tomcat·mybatis·安全开发
Jay_Franklin1 小时前
SRIM通过python计算dap
开发语言·python
是一个Bug1 小时前
Java基础50道经典面试题(四)
java·windows·python
Slow菜鸟1 小时前
Java基础架构设计(三)| 通用响应与异常处理(分布式应用通用方案)
java·开发语言
吴佳浩2 小时前
Python入门指南(七) - YOLO检测API进阶实战
人工智能·后端·python
消失的旧时光-19432 小时前
401 自动刷新 Token 的完整架构设计(Dio 实战版)
开发语言·前端·javascript
wadesir2 小时前
Rust中的条件变量详解(使用Condvar的wait方法实现线程同步)
开发语言·算法·rust
tap.AI2 小时前
RAG系列(二)数据准备与向量索引
开发语言·人工智能
阿蒙Amon2 小时前
C#每日面试题-重写和重载的区别
开发语言·c#