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

相关推荐
小此方6 分钟前
Re:从零开始的 C++ STL篇(三)string的疑难问题详细解析:深拷贝,写时拷贝,三个swap
开发语言·c++
计算机徐师兄6 分钟前
Python基于Django的图片推荐系统(附源码,文档说明)
python·django·网络爬虫·图片推荐系统·python图片推荐系统·python图片推荐·图片推荐
Linux猿11 分钟前
基于Python的图书管理系统(可执行源码+详细报告+详细注释+运行步骤)
开发语言·python·毕业设计·课程设计·管理系统·图书管理系统项目
lanbing12 分钟前
在Mac OS系统中安装Go语言环境教程
开发语言·后端·golang
奥特曼_ it14 分钟前
【NLP舆情分析】基于python微博舆情分析可视化系统(完整系统源码+数据库+详细部署教程)✅
数据库·python·自然语言处理
sensen_kiss16 分钟前
Python安装与环境配置全程详细教学(包含Windows版和Mac版)
开发语言·python·pycharm
程序员敲代码吗18 分钟前
嵌入式C++开发注意事项
开发语言·c++·算法
Dr.Kun20 分钟前
【鲲码园Python】基于yolov11的番茄成熟度检测系统
开发语言·python·yolo
白开水+21 分钟前
AI学习-第二天
python·学习
无心水22 分钟前
17、Go协程通关秘籍:主协程等待+多协程顺序执行实战解析
开发语言·前端·后端·算法·golang·go·2025博客之星评选投票