【python】【conda】【任务】【管理环境】

本文档详细介绍了如何使用conda进行环境管理,包括创建、更新、克隆、激活、停用、查看环境、安装和卸载软件包、设置环境变量、共享和恢复环境,以及删除环境等操作。通过这些步骤,可以帮助用户有效地管理和维护不同版本的Python和软件包环境。
摘要由CSDN通过智能技术生成

目录

Managing environments 

管理环境

1 Creating an environment with commands

1 使用命令创建环境

2 Creating an environment from an environment.yml file

2 从environment.yml文件创建环境

3 Specifying a location for an environment

3 指定环境的位置

4 Updating an environment

4 更新环境

5 Cloning an environment

5 克隆环境

6 Building identical conda environments

6 构建相同的conda环境

7 Activating an environment

7 激活环境

7.1 Conda init 

7.1 Conda初始化

7.2 Nested activation 

7.2 嵌套激活

7.3 Environment variable for DLL loading verification

7.3 DLL加载验证的环境变量

8 Deactivating an environment

8 停用环境

9 Determining your current environment

9 确定当前环境

10 Viewing a list of your environments

10 查看您的环境列表

11 Viewing a list of the packages in an environment

11 查看环境中的软件包列表

12 Using pip in an environment

12 在环境中使用pip

13 Setting environment variables

13 设置环境变量

14 Saving environment variables

14 保存环境变量

Windows 窗口

macOS和Linux

15 Sharing an environment 

15 共享环境

15.1 Exporting the environment.yml file

15.1 导出environment.yml文件

15.2 Exporting an environment file across platforms

15.2 跨平台导出环境文件

15.3 Creating an environment file manually

15.3 手动创建环境文件

16 Restoring an environment

16 恢复环境

17 Removing an environment

17 删除环境

Managing environments 

管理环境

With conda, you can create, export, list, remove, and update environments that have different versions of Python and/or packages installed in them.

Switching or moving between environments is called activating the environment.

You can also share an environment file.
使用conda,您可以创建、导出、列出、删除和更新安装了不同版本Python和/或软件包的环境。

在环境之间切换或移动称为激活环境。

您还可以共享环境文件。

There are many options available for the commands described on this page. For a detailed reference on all available commands, see commands.
有许多选项可用于本页所述的命令。有关所有可用命令的详细参考,请参见命令。

1 Creating an environment with commands

1 使用命令创建环境

Use the terminal for the following steps:
使用终端执行以下步骤:

  1. To create an environment:
    创建环境:

    conda create --name <my-env>
    

    Replace <my-env> with the name of your environment.
    将 <my-env> 替换为您的环境名称。

  2. When conda asks you to proceed, type y:
    当conda要求您继续时,键入 y :

    proceed ([y]/n)?
    

    This creates the myenv environment in /envs/. No packages will be installed in this environment.
    这将在 /envs/ 中创建myenv环境。此环境中不会安装任何软件包。

  3. To create an environment with a specific version of Python:
    要使用特定版本的Python创建环境,请执行以下操作:

    conda create -n myenv python=3.9
    
  4. To create an environment with a specific package:
    要创建具有特定包的环境,请执行以下操作:

    conda create -n myenv scipy
    

    or:

    conda create -n myenv python
    conda install -n myenv scipy
    
  5. To create an environment with a specific version of a package:
    要使用特定版本的包创建环境,请执行以下操作:

    conda create -n myenv scipy=0.17.3
    

    or:

    conda create -n myenv python
    conda install -n myenv scipy=0.17.3
    
  6. To create an environment with a specific version of Python and multiple packages:
    要使用特定版本的Python和多个包创建环境,请执行以下操作:

    conda create -n myenv python=3.9 scipy=0.17.3 astroid babel
    

    Tip

    Install all the programs that you want in this environment at the same time. Installing one program at a time can lead to dependency conflicts.
    在此环境中同时安装所需的所有程序。一次安装一个程序可能会导致依赖性冲突。

To automatically install pip or another program every time a new environment is created, add the default programs to the create_default_packages section of your .condarc configuration file. The default packages are installed every time you create a new environment. If you do not want the default packages installed in a particular environment, use the --no-default-packages flag:
要在每次创建新环境时自动安装pip或其他程序,请将默认程序添加到 .condarc 配置文件的create_default_packages部分。每次创建新环境时都会安装默认包。如果您不希望在特定环境中安装默认包,请使用 --no-default-packages 标志:

conda create --no-default-packages -n myenv python

Tip

You can add much more to the conda create command. For details, run conda create --help.
您可以向 conda create 命令添加更多内容。有关详细信息,请运行 conda create --help 。

2 Creating an environment from an environment.yml file

2 从environment.yml文件创建环境

Use the terminal for the following steps:
使用终端执行以下步骤:

  1. Create the environment from the environment.yml file:
    从 environment.yml 文件创建环境:

    conda env create -f environment.yml
    

    The first line of the yml file sets the new environment's name. For details see Creating an environment file manually.
    yml 文件的第一行设置新环境的名称。有关详细信息,请参见手动创建环境文件。

  2. Activate the new environment: conda activate myenv
    启用新环境: conda activate myenv

  3. Verify that the new environment was installed correctly:
    验证新环境是否已正确安装:

    conda env list
    

    You can also use conda info --envs.
    你也可以使用 conda info --envs 。

3 Specifying a location for an environment

3 指定环境的位置

You can control where a conda environment lives by providing a path to a target directory when creating the environment. For example, the following command will create a new environment in a subdirectory of the current working directory called envs:
您可以通过在创建conda环境时提供目标目录的路径来控制该环境的位置。例如,以下命令将在当前工作目录的目录中创建一个名为 envs 的新环境:

conda create --prefix ./envs jupyterlab=3.2 matplotlib=3.5 numpy=1.21

You then activate an environment created with a prefix using the same command used to activate environments created by name:
然后,使用与激活按名称创建的环境相同的命令激活使用前缀创建的环境:

conda activate ./envs

Specifying a path to a subdirectory of your project directory when creating an environment has the following benefits:
在创建环境时,使用项目目录的目录路径具有以下优点:

  • It makes it easy to tell if your project uses an isolated environment by including the environment as a subdirectory.
    通过将环境包含为一个隔离环境,可以很容易地判断您的项目是否使用隔离环境。

  • It makes your project more self-contained as everything, including the required software, is contained in a single project directory.
    它使您的项目更加独立,因为所有内容(包括所需的软件)都包含在一个项目目录中。

An additional benefit of creating your project’s environment inside a subdirectory is that you can then use the same name for all your environments. If you keep all of your environments in your envs folder, you’ll have to give each environment a different name.
在EJB中创建项目环境的另一个好处是,您可以对所有环境使用相同的名称。如果您将所有环境保存在 envs 文件夹中,则必须为每个环境给予不同的名称。

There are a few things to be aware of when placing conda environments outside of the default envs folder.
将conda环境放置在默认的 envs 文件夹之外时,需要注意一些事情。

  1. Conda can no longer find your environment with the --name flag. You’ll generally need to pass the --prefix flag along with the environment’s full path to find the environment.
    Conda无法再找到带有 --name 标志的环境。通常需要将 --prefix 标志沿着环境的完整路径一起传递,以查找环境。

  2. Specifying an install path when creating your conda environments makes it so that your command prompt is now prefixed with the active environment’s absolute path rather than the environment’s name.
    在创建conda环境时设置安装路径,使命令提示符现在以活动环境的绝对路径而不是环境名称为前缀。

After activating an environment using its prefix, your prompt will look similar to the following:
使用前缀激活环境后,提示符将类似于以下内容:

(/absolute/path/to/envs) $

This can result in long prefixes:
这可能导致长前缀:

(/Users/USER_NAME/research/data-science/PROJECT_NAME/envs) $

To remove this long prefix in your shell prompt, modify the env_prompt setting in your .condarc file:
要删除shell提示符中的长前缀,请修改 .condarc 文件中的env_prompt设置:

conda config --set env_prompt '({name})'
conda config --set env_prompt“({name})”

This will edit your .condarc file if you already have one or create a .condarc file if you do not.
这将编辑您的 .condarc 文件(如果您已经有一个)或创建一个 .condarc 文件(如果您没有)。

Now your command prompt will display the active environment’s generic name, which is the name of the environment's root folder:
现在,命令提示符将显示活动环境的通用名称,即环境根文件夹的名称:

$ cd project-directory
$ conda activate ./env
(env) project-directory $

4 Updating an environment

4 更新环境

You may need to update your environment for a variety of reasons. For example, it may be the case that:
由于各种原因,您可能需要更新您的环境。例如,情况可能是:

  • one of your core dependencies just released a new version (dependency version number update).
    你的核心依赖之一刚刚发布了一个新版本(依赖版本号更新)。

  • you need an additional package for data analysis (add a new dependency).
    你需要一个额外的数据分析包(添加一个新的依赖项)。

  • you have found a better package and no longer need the older package (add new dependency and remove old dependency).
    你已经找到了一个更好的包,不再需要旧的包(添加新的依赖项并删除旧的依赖项)。

If any of these occur, all you need to do is update the contents of your environment.yml file accordingly and then run the following command:
如果发生任何这些情况,您需要做的就是相应地更新 environment.yml 文件的内容,然后运行以下命令:

conda env update --file environment.yml --prune

Note 注意

The --prune option causes conda to remove any dependencies that are no longer required from the environment.
--prune 选项使conda删除环境中不再需要的任何依赖项。

5 Cloning an environment

5 克隆环境

Use the terminal for the following steps:
使用终端执行以下步骤:

You can make an exact copy of an environment by creating a clone of it:
您可以通过创建环境的克隆来制作环境的精确副本:

conda create --name myclone --clone myenv

Note 注意

Replace myclone with the name of the new environment. Replace myenv with the name of the existing environment that you want to copy.
将 myclone 替换为新环境的名称。将 myenv 替换为您要复制的现有环境的名称。

To verify that the copy was made:
要验证是否制作了副本:

conda info --envs

In the environments list that displays, you should see both the source environment and the new copy.
在显示的环境列表中,您应该可以看到源环境和新副本。

6 Building identical conda environments

6 构建相同的conda环境

You can use explicit specification files to build an identical conda environment on the same operating system platform, either on the same machine or on a different machine.
您可以使用显式规范文件在相同的操作系统平台上构建相同的conda环境,无论是在同一台机器上还是在不同的机器上。

Use the terminal for the following steps:
使用终端执行以下步骤:

  1. Run conda list --explicit to produce a spec list such as:
    运行 conda list --explicit 以生成规格列表,例如:

    # This file may be used to create an environment using:
    # $ conda create --name <env> --file <this file>
    # platform: osx-64
    @EXPLICIT
    https://repo.anac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

资源存储库

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

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

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

打赏作者

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

抵扣说明:

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

余额充值