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

相关推荐
默_笙4 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!4 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003964 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技4 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读4 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时4 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车4 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊4 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1234 天前
2026年第38周GitHub趋势周报
python·科技·github