Python 如何激活 Anaconda 环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20081338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:28:22  来源:igfitidea点击:

How to activate an Anaconda environment

pythonvirtualenvanacondaconda

提问by pandita

I'm on Windows 8, using Anaconda 1.7.5 64bit.

我在 Windows 8 上,使用 Anaconda 1.7.5 64 位。

I created a new Anaconda environment with

我创建了一个新的 Anaconda 环境

conda create -p ./test python=2.7 pip

conda create -p ./test python=2.7 pip

from C:\Pr\TEMP\venv\.

C:\Pr\TEMP\venv\.

This worked well (there is a folder with a new python distribution). conda tells me to type

这很有效(有一个带有新 python 发行版的文件夹)。conda 告诉我输入

activate C:\PR\TEMP\venv\test

activate C:\PR\TEMP\venv\test

to activate the environment, however this returns:

激活环境,但是这会返回:

No environment named "C:\PR\temp\venv\test" exists in C:\PR\Anaconda\envs

No environment named "C:\PR\temp\venv\test" exists in C:\PR\Anaconda\envs

How can I activate the environment? What am I doing wrong?

如何激活环境?我究竟做错了什么?

采纳答案by FZNB

If this happens you would need to set the PATH for your environment (so that it gets the right Python from the environment and Scripts\ on Windows).

如果发生这种情况,您需要为您的环境设置 PATH(以便它从环境和 Windows 上的 Scripts\ 中获取正确的 Python)。

Imagine you have created an environment called py33 by using:

假设您使用以下命令创建了一个名为 py33 的环境:

conda create -n py33 python=3.3 anaconda

Here the folders are created by default in Anaconda\envs, so you need to set the PATH as:

这里的文件夹默认创建在 Anaconda\envs 中,因此您需要将 PATH 设置为:

set PATH=C:\Anaconda\envs\py33\Scripts;C:\Anaconda\envs\py33;%PATH%

Now it should work in the command window:

现在它应该在命令窗口中工作:

activate py33

The line above is the Windows equivalent to the code that normally appears in the tutorials for Mac and Linux:

上面这行代码是 Windows 中的代码,通常出现在 Mac 和 Linux 的教程中:

$ source activate py33

More info: https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/8T8i11gO39U

更多信息:https: //groups.google.com/a/continuum.io/forum/#!topic/anaconda/ 8T8i11gO39U

Does `anaconda` create a separate PYTHONPATH variable for each new environment?

`anaconda` 是否为每个新环境创建一个单独的 PYTHONPATH 变量?

回答by bdshadow

As you can see from the error message the paths, that you specified, are wrong. Try it like this:

正如您从错误消息中看到的,您指定的路径是错误的。像这样尝试:

activate ..\..\temp\venv\test

However, when I needed to install Anaconda, I downloaded it from hereand installed it to the default paths (C:\Anaconda), than I put this path to the environment variables, so now Anacondas interpreter is used as default. If you are using PyCharm, for example, you can specify the interpreter there directly.

但是,当我需要安装Anaconda时,我从这里下载并安装到默认路径(C:\Anaconda),而不是将此路径放在环境变量中,因此现在使用Anacondas解释器作为默认值。例如,如果您使用 PyCharm,则可以直接在那里指定解释器。

回答by Nelson

I was having the same, a fix seems to have been made in the source.

我有同样的情况,似乎已经在源代码中进行了修复。

回答by user5886017

Below is how it worked for me

以下是它对我的工作方式

  1. C:\Windows\system32>set CONDA_ENVS_PATH=d:\your\location
  2. C:\Windows\system32>conda info
  1. C:\Windows\system32>set CONDA_ENVS_PATH=d:\your\location
  2. C:\Windows\system32>conda 信息

Shows new environment path

显示新的环境路径

  1. C:\Windows\system32>conda create -n YourNewEnvironment --clone=root
  1. C:\Windows\system32>conda create -n YourNewEnvironment --clone=root

Clones default root environment

克隆默认根环境

  1. C:\Windows\system32>activate YourNewEnvironment
  1. C:\Windows\system32>激活你的新环境

Deactivating environment "d:\YourDefaultAnaconda3"... Activating environment "d:\your\location\YourNewEnvironment"...

停用环境“d:\YourDefaultAnaconda3”... 激活环境“d:\your\location\YourNewEnvironment”...

  1. [YourNewEnvironment] C:\Windows\system32>conda info -e

  1. [你的新环境] C:\Windows\system32>conda info -e

conda environments: #

conda 环境:#

YourNewEnvironment
* d:\your\location\YourNewEnvironment

YourNewEnvironment
* d:\your\location\YourNewEnvironment

root d:\YourDefaultAnaconda3

根 d:\YourDefaultAnaconda3

回答by Bas Swinckels

Note that the command for activating an environment has changed in Conda version 4.4. The recommended way of activating an environment is now conda activate myenvinstead of source activate myenv. To enable the new syntax, you should modify your .bashrcfile. The line that currently reads something like

请注意,用于激活环境的命令在 Conda 4.4 版中已更改。激活一个环境的推荐的方法是现在conda activate myenv,而不是source activate myenv。要启用新语法,您应该修改您的 .bashrc文件。当前读取的行类似于

export PATH="<path_to_your_conda_install>/bin:$PATH"

Should be changed to

应该改为

. <path_to_your_conda_install>/etc/profile.d/conda.sh

This only adds the condacommand to the path, but does not yet activate the baseenvironment (which was previously called root). To do also that, add another line

这只会将conda命令添加到路径中,但尚未激活base环境(之前称为root)。要做到这一点,请添加另一行

conda activate base

after the first command. See all the details in Anaconda's blog postfrom December 2017. (I think that this page is currently missing a newline between the two lines, it says .../conda.shconda activate base).

在第一个命令之后。查看2017 年 12 月Anaconda 博客文章中的所有详细信息。(我认为此页面目前在两行之间缺少换行符,它说.../conda.shconda activate base)。

(This answer is valid for Linux, but it might be relevant for Windows and Mac as well)

(此答案适用于 Linux,但也可能适用于 Windows 和 Mac)

回答by Ilia Chigogidze

Use cmd instead of Powershell!I spent 2 hours before I switched to cmd and then it worked!

使用 cmd 而不是 Powershell!我在切换到 cmd 之前花了 2 个小时,然后它起作用了!

create Environment:

创建环境:

conda create -n your_environment_name

see list of conda environments:

查看 conda 环境列表:

conda env list

activate your environment:

激活您的环境:

conda activate your_environment_name

That's all folks

这就是所有人

回答by Rahul Verma

let's assume your environment name is 'demo'and you are using anaconda and want to create a virtual environment:

让我们假设您的环境名称是“demo”,并且您正在使用 anaconda 并想要创建一个虚拟环境:

(if you want python3)

(如果你想要python3)

    conda create -n demo python=3

(if you want python2)

(如果你想要python2)

    conda create -n demo python=2

After running above command you have to activate the environment by bellow command:

运行上述命令后,您必须通过以下命令激活环境:

    source activate demo 

回答by Alex Granovsky

I've tried to activate env from Jenkins job (in bash) with conda activate baseand it failed, so after many tries, this one worked for me (CentOS 7) :

我试图从 Jenkins 工作(在 bash 中)激活 env conda activate base并且它失败了,所以经过多次尝试,这个对我有用(CentOS 7):

source /opt/anaconda2/bin/activate base

回答by Qin Heyang

For me, using Anaconda Prompt instead of cmd or PowerShell is the key.

对我来说,使用 Anaconda Prompt 而不是 cmd 或 PowerShell 是关键。

In Anaconda Prompt, all I need to do is activate XXX

在 Anaconda Prompt 中,我需要做的就是 activate XXX

回答by Simba

All the former answers seem to be outdated.

以前的所有答案似乎都已过时。

conda activatewas introduced in conda4.4 and 4.6.

conda activateconda4.4 和 4.6 中引入。

conda activate: The logic and mechanisms underlying environment activation have been reworked. With conda 4.4, conda activateand conda deactivateare now the preferred commands for activating and deactivating environments. You'll find they are much more snappy than the source activateand source deactivatecommands from previous conda versions. The conda activatecommand also has advantages of (1) being universal across all OSes, shells, and platforms, and (2) not having path collisions with scripts from other packages like python virtualenv's activate script.

conda activate:环境激活的逻辑和机制已被重新设计。与康达4.4,conda activate并且conda deactivate是现在用于激活和停用环境的首选命令。您会发现它们比以前的 conda 版本中的source activatesource deactivate命令快得多。该conda activate命令还具有以下优点:(1)在所有操作系统、shell和平台上通用,以及 (2) 不会与来自其他包(如 python virtualenv 的激活脚本)的脚本发生路径冲突。

Examples

例子

conda create -n venv-name python=3.6
conda activate -n venv-name
conda deactivate

These new sub-commands are available in "Aanconda Prompt" and "Anaconda Powershell Prompt" automatically. To use conda activatein every shell(normal cmd.exeand powershell), check expose condacommand in every shell on Windows.

这些新的子命令在“Aanconda Prompt”和“Anaconda Powershell Prompt”中自动可用。要conda activate每个 shell(普通cmd.exe和 powershell)中使用,请在 Windows 上的每个 shell 中检查公开conda命令

References

参考