首页 conda安装与使用
文章
取消

conda安装与使用

介绍

  Conda 是一个开源的软件包管理系统和环境管理系统,用于安装多个版本的软件包及其依赖关系,并在它们之间轻松切换。Conda 是为 Python 程序创建的,适用于 Linux,OS X 和Windows,也可以打包和分发其他软件。类似的软件包管理器还有 venv, pyenv ,virtualenv 等。

  • venv:Python 3.3及更高版本内置的模块,用于创建虚拟环境,不能管理python版本。
  • virtualenv:Python 2.7及更高版本内置的模块,用于创建虚拟环境。
  • pyenv:一个Python版本管理工具,不是包管理器,但可以轻松切换多个Python版本。
  • conda:一个包管理器,可以管理多个版本的Python,同时可以创建虚拟环境。同时还可以管理其他语言,如R、Java等。

版本

  • conda:命令行工具,用于管理包和环境。
  • miniconda:conda的精简版,只包含conda和python。
  • anaconda:conda的完整版,包含conda、python、科学计算包。

安装

  这里选用了miniconda,因为anaconda太大了,而且除了多了很多的包就是多了一个图形界面,一般也用不到,所以这里就选了miniconda。使用命令行安装

windows

1
2
3
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe -o miniconda.exe
start /wait "" miniconda.exe /S
del miniconda.exe

macOS

1
2
3
4
5
6
7
8
mkdir -p ~/miniconda3
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -o ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh

#安装后,初始化新安装的Miniconda。以下命令为bash和zsh shell初始化:
~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh

linux

1
2
3
4
5
6
7
8
mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh

# 安装后,初始化新安装的Miniconda。以下命令为bash和zsh shell初始化:
~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh

使用

  安装完成后,可以通过–help命令查看帮助信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$ conda --help
usage: conda [-h] [-v] [--no-plugins] [-V] COMMAND ...

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

options:
  -h, --help          Show this help message and exit.
  -v, --verbose       Can be used multiple times. Once for detailed output, twice for INFO logging, thrice for DEBUG logging, four
                      times for TRACE logging.
  --no-plugins        Disable all plugins that are not built into conda.
  -V, --version       Show the conda version number and exit.

commands:
  The following built-in and plugins subcommands are available.

  COMMAND
    activate          Activate a conda environment.
    build             Build conda packages from a conda recipe.
    clean             Remove unused packages and caches.
    compare           Compare packages between conda environments.
    config            Modify configuration values in .condarc.
    content-trust     Signing and verification tools for Conda
    convert           Convert pure Python packages to other platforms (a.k.a., subdirs).
    create            Create a new conda environment from a list of specified packages.
    deactivate        Deactivate the current active conda environment.
    debug             Debug the build or test phases of conda recipes.
    develop           Install a Python package in 'development mode'. Similar to `pip install --editable`.
    doctor            Display a health report for your environment.
    env               See `conda env --help`.
    index             Update package index metadata files. Pending deprecation, use https://github.com/conda/conda-index instead.
    info              Display information about current conda install.
    init              Initialize conda for shell interaction.
    inspect           Tools for inspecting conda packages.
    install           Install a list of packages into a specified conda environment.
    list              List installed packages in a conda environment.
    metapackage       Specialty tool for generating conda metapackage.
    notices           Retrieve latest channel notifications.
    pack              See `conda pack --help`.
    package           Create low-level conda packages. (EXPERIMENTAL)
    remove (uninstall)
                      Remove a list of packages from a specified conda environment.
    rename            Rename an existing environment.
    render            Expand a conda recipe into a platform-specific recipe.
    repo              See `conda repo --help`.
    repoquery         Advanced search for repodata.
    run               Run an executable in a conda environment.
    search            Search for packages and display associated information using the MatchSpec format.
    server            See `conda server --help`.
    skeleton          Generate boilerplate conda recipes.
    token             See `conda token --help`.
    update (upgrade)  Update conda packages to the latest compatible version.
    verify            See `conda verify --help`.

  上面是conda的命令行帮助文档,我和它之间就差一个好的翻译。下面介绍一些常用的命令:

环境管理

查看虚拟环境

1
conda env list

创建虚拟环境

1
conda create --name <env_name> python=3.9

激活虚拟环境

1
conda activate <env_name>

虚拟环境安装Python库

1
2
3
4
5
conda install pytest
# 也可以指定版本
conda install pytest=3.9
# 也可以使用pip
pip install pytest

环境迁移

拷贝环境

1
conda create --name <new_env_name> --clone <old_env_name>

导出环境包列表

1
2
3
4
# 导出
conda env export > environment.yml
# 导入
conda env create -f environment.yml

离线部署

1
2
3
4
5
6
7
8
9
10
# 安装打包工具:
conda install conda-pack 
# 或
pip install conda-pack
# 打包虚拟环境
conda pack -n <env_name> 
# 创建新环境目录
mkdir <env_name>
# 还原环境
tar -zxvf <env_name>.tar.gz -C <env_name>

Conda-Pack 打包环境方式在目标计算机无法联网或者网络不畅时很好用,而Conda导出 environment.yml 的方式非常适合在不同平台和操作系统之间重新创建环境。

结束语

  conda 还可以与PyCharm等开发工具结合使用,在PyCharm中创建虚拟环境后,可以自动激活虚拟环境。Anaconda 安装后还有上面的图形界面,但是我觉得没有啥用,所有之后安装的都是 Miniconda。

本文由作者按照 CC BY 4.0 进行授权

Prometheus(三):PromQL

Stable Diffusion:部署