bash Conda 激活/停用破坏 PATH 的环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45095500/
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-09-18 16:16:40  来源:igfitidea点击:

Conda activate/deactivate environment breaking PATH

pythonbashpathanacondaconda

提问by Brad Allen

I am finding that when I create a Conda environment source activate environmentand then deactivate that environment source deactivate environment, my native bash commands no longer work.

我发现当我创建 Conda 环境source activate environment然后停用该环境时source deactivate environment,我的本机 bash 命令不再起作用。

I've attached an image of ls working before enabling an environment and then not afterwards. I assume that this is something because of my PATH - but I am a novice and am not sure specifically what to fix. I am doing this in Git Bash on Windows. Anaconda 3 and Python 3.5.

我在启用环境之前附加了 ls 工作的图像,然后没有。我认为这是因为我的路径 - 但我是一个新手并且不确定具体要解决什么问题。我正在 Windows 上的 Git Bash 中执行此操作。蟒蛇 3 和 Python 3.5。

Please work ls, come on

请工作,来吧

采纳答案by Lohit Gupta

Most probably your conda environment is replacing the parameters and environment variables defined in your local bash profile. I am sure if you open a new environment(bash session), you will find everything is working fine.

很可能您的 conda 环境正在替换本地 bash 配置文件中定义的参数和环境变量。我敢肯定,如果您打开一个新环境(bash 会话),您会发现一切正常。

I would suggest you add a command to reload bash profile after deactivating the environment, so that the profile variable values will again be applied. It should solve the problem.

我建议您在停用环境后添加一个命令来重新加载 bash 配置文件,以便再次应用配置文件变量值。它应该可以解决问题。

回答by Richard Plevin

There are a few bugs in the activate/deactivate scripts, which I've fixed in versions I call activate.cygwin and deactivate.cygwin.

激活/停用脚本中存在一些错误,我已在称为 activate.cygwin 和deactivate.cygwin 的版本中修复了这些错误。

  • activate.cygwin: fixes 2 places where the conda command returns a string with a return "\r" at the end that confounds processing.

  • deactivate.cygwin: fixes conversion of /cygdrive/... to avoid error CondaValueError: Could not find environment: /cygdrivec:\Users\rjp\Anaconda2\envs...

  • activate.cygwin:修复了 conda 命令在结尾处返回字符串“\r”的 2 个地方,这会混淆处理。

  • Deactivate.cygwin:修复了 /cygdrive/... 的转换以避免错误 CondaValueError:找不到环境:/cygdrivec:\Users\rjp\Anaconda2\envs...

I've posted the modified scripts to https://bitbucket.org/snippets/plevin/. See instructions at the top of activate.cygwin.

我已将修改后的脚本发布到https://bitbucket.org/snippets/plevin/。请参阅 activate.cygwin 顶部的说明。

回答by joaopcoelho

The correct way to deactivate a conda environment is to run conda deactivate, rather than source deactivate environment

停用 conda 环境的正确方法是运行conda deactivate,而不是source deactivate environment

Source: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=activate#deactivating-an-environment

来源:https: //docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=activate#deactivation-an-environment

回答by chenxin

I use Git Bash in Windows 10 and encountered the same problem too. The previous answer does not work for me, probably because I don't have any path set in ~/.bash_profile and ~/.bashrc.

我在 Windows 10 中使用 Git Bash 也遇到了同样的问题。上一个答案对我不起作用,可能是因为我没有在 ~/.bash_profile 和 ~/.bashrc 中设置任何路径。

I checked my path variable in a fresh git bash, it looks like:

我在一个新的 git bash 中检查了我的路径变量,它看起来像:

/c/Users/chen_x/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/chen_x/bin....(more)

/c/Users/chen_x/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/chen_x/bin... 。(更多的)

After . C:/Users/chen_x/Miniconda3/Scripts/activate C:/Users/chen_x/Miniconda3and . activate snowflakes, the path becomes:

. C:/Users/chen_x/Miniconda3/Scripts/activate C:/Users/chen_x/Miniconda3和之后. activate snowflakes,路径变为:

/C/Users/chen_x/Miniconda3/envs/snowflakes:(blalbla..):C:\Users\chen_x\Miniconda3\Library\bin;C:\Users\chen_x\Miniconda3;....(more)

/C/Users/chen_x/Miniconda3/envs/snowflakes:(balbla..):C:\Users\chen_x\Miniconda3\Library\bin;C:\Users\chen_x\Miniconda3;....(more)

I assume that git bash does not recognize pathes like C:\program files..., then added the following line to C:/Users/chen_x/Miniconda3/Scripts/activateright after the last EXPORT PATH=command (about line 78):

我假设 git bash 无法识别路径C:\program files...,然后C:/Users/chen_x/Miniconda3/Scripts/activate在最后一个EXPORT PATH=命令之后添加以下行(大约第 78 行):

export PATH="$($_CONDA_PYTHON -c "import re; p=re.sub(r'\\', r'/', r'$PATH'); p=re.sub('(;|:)([A-Z]):', lambda m: ':/'+m.group(2).lower(), p); print(p)")"

It works.

有用。

By the way, I create a ~/condaenv script to start conda environment:

顺便说一下,我创建了一个 ~/condaenv 脚本来启动 conda 环境:

#!/bin/bash
. C:/Users/chen_x/Miniconda3/Scripts/activate C:/Users/chen_x/Miniconda3

It would be convinent to :

这将是方便的:

. ~/condaenv
. activate snowflakes