简体   繁体   English

在 windows 上使用带有 cygwin 的 anaconda 环境

[英]Using anaconda environments with cygwin on windows

Trying to set up environments with anaconda through the cygwin interface on Windows NT, and failing.尝试通过 Windows NT 上的 cygwin 界面使用 anaconda 设置环境,但失败了。

Creating environments ( conda create -n test_env ) works fine.创建环境( conda create -n test_env )工作正常。 But activate test_env fails.但是activate test_env失败。

I tried hacking it with:我尝试用以下方法破解它:

export PATH=/cygdrive/c/users/nick/anaconda3/envs/test:$PATH

This fixes some behavior ( which python points to the right python).这修复了一些行为( which python指向正确的 python)。 But if I then do a "conda install" command, it installs into the root anaconda directory, not the environment.但是,如果我执行“conda install”命令,它会安装到 anaconda 根目录中,而不是环境中。 Perhaps the export is local to the bash session, and conda calls a different version of PATH?也许导出是 bash 会话的本地导出,而 conda 调用了不同版本的 PATH? There a way to make the modification of PATH global?有没有办法让 PATH 的修改成为全局的?

After wrestling with the problem for quite some time, I think I've achieved a reasonable and workable method to integrate Anaconda's python (and associated environments) into Cygwin.在与这个问题搏斗了一段时间之后,我认为我已经找到了一种合理可行的方法来将 Anaconda 的 python(和相关环境)集成到 Cygwin 中。 Assuming you have both Cygwin and Anaconda working independently, to access all of the Anaconda tools from Cygwin, the following setup in .bash_profile seems to do the trick.假设您让 Cygwin 和 Anaconda 独立工作,要从 Cygwin 访问所有 Anaconda 工具, .bash_profile的以下设置似乎可以解决问题。 (I have only included those portions of .bash_profile relevant to the integration... hoping I did not miss something inadvertently.) (我只包含了与集成相关的.bash_profile那些部分......希望我没有无意中错过一些东西。)

This setup essentially does three things.这个设置基本上做了三件事。 First, the user needs to explicitly set the directory $CONDA_BASE_DIR to be the location where the base environment for conda/anaconda/miniconda was installed.首先,用户需要将目录$CONDA_BASE_DIR显式设置$CONDA_BASE_DIR /anaconda/miniconda 的基本环境的安装位置。 Second, there is a functionality in .bash_profile to keep track of the current conda environment using a shell variable $CONDA_DEFAULT_ENV .其次, .bash_profile有一个功能可以使用 shell 变量$CONDA_DEFAULT_ENV跟踪当前的 conda 环境。 And finally, we define an alias cyg-conda and a function cyg-activate to be used as replacement commands for the standard conda and activate commands.最后,我们定义别名cyg-conda和函数cyg-activate用作标准condaactivate命令的替换命令。 Please note that the variable name $CONDA_DEFAULT_ENV is special, and used internally by the actual conda command.请注意变量名$CONDA_DEFAULT_ENV是特殊的,由实际的conda命令在内部使用。

Using this setup, I am able to use cyg-conda and cyg-activate in the same way I would typically use conda and activate at the Anaconda command prompt, while making the environments available to my Cygwin bash shell.使用此设置,我能够以与通常在 Anaconda 命令提示符下使用condaactivate相同的方式使用cyg-condacyg-activate ,同时使环境可用于我的 Cygwin bash shell。

Certainly open to suggestions for improvements, etc.当然愿意接受改进建议等。

###############################################################################

#  Anaconda Environment Selection - Plese set CONDA_BASE_DIR to the directory
#  containing the base installation of anaconda/miniconda.

export CONDA_BASE_DIR=/cygdrive/c/Users/Patrick/Miniconda3

#  Proxy Servers & Network Setup (if needed)

export HTTP_PROXY=
export HTTPS_PROXY=

#  IMPORTANT - Ignore carriage returns when using a Cygwin environment.

export SHELLOPTS
set -o igncr

###############################################################################

#  Manage conda environments for Python.  We check the environment variable
#  $CONDA_DEFAULT_ENV to see which environment is desired.  The default (root)
#  environment will be chosen if nothing is specified.  Note that this variable
#  will be explicitly managed by the cyg-activate ( ) function we have defined
#  below, specifically for the purpose of changing environments.  The root
#  environment is also handled slightly different from the others when it comes
#  to setting the CONDA_DEFAULT_ENV variable.

if [ ${CONDA_DEFAULT_ENV} ] && [ ${CONDA_DEFAULT_ENV} != 'root' ] 
then
    #  SELECT ONE OF THE NON-DEFAULT ENVIRONMENTS
    export CONDA_PREFIX=${CONDA_BASE_DIR}/envs/${CONDA_DEFAULT_ENV}
else
    #  SELECT THE DEFAULT ENVIRONMENT (and set CONDA_DEFAULT_ENV full path)
    export CONDA_DEFAULT_ENV=root
    export CONDA_PREFIX=${CONDA_BASE_DIR}
fi

###############################################################################

#  Define cyg-conda and cyg-activate to facilitate management of conda.

alias cyg-conda=${CONDA_BASE_DIR}/Scripts/conda.exe

cyg-activate() {
    export CONDA_DEFAULT_ENV=$1
    source ~/.bash_profile
    cyg-conda info --envs
}

###############################################################################

#  PATH - ALl of the anaconda/miniconda path entries appear first.

PATH=
PATH=$PATH:$CONDA_PREFIX
PATH=$PATH:$CONDA_PREFIX/Library/mingw-w64/bin
PATH=$PATH:$CONDA_PREFIX/Library/usr/bin
PATH=$PATH:$CONDA_PREFIX/Library/bin
PATH=$PATH:$CONDA_PREFIX/Scripts
PATH=$PATH:$HOME/scripts
PATH=$PATH:$HOME/local/bin
PATH=$PATH:/usr/local/bin
PATH=$PATH:/usr/bin

export PATH

###############################################################################

As of conda 4.4 the activate & deactivate commands are supported in cygwin with the below syntax (the linked documentation also provides best practices on adding conda to PATH which is worth checking out):conda 4.4 开始,cygwin 支持使用以下语法激活和停用命令(链接的文档还提供了将 conda 添加到PATH最佳实践,值得一试):

conda activate <name-of-environment-to-activate>
conda deactivate

However there is a bug that prevents these from working out of the box which is that the bash scripts that cygwin makes use of all have Windows line endings (CRLF).但是,有一个错误会阻止它们开箱即用,即 cygwin 使用的 bash 脚本都具有 Windows 行尾(CRLF)。 To resolve this there are a couple of options:要解决此问题,有几个选项:

  1. add the following to your .bash_profile or .bashrc (as is done in the script in @patrickkelly's answer):将以下内容添加到您的.bash_profile.bashrc (如@patrickkelly 的答案中的脚本中所做的那样):

     if [[ "${OSTYPE}" == 'cygwin' ]]; then set -o igncr export SHELLOPTS fi
  2. Change the line endings of the relevant files to Unix style (LF) with a tool like dos2unix .使用dos2unix类的工具将相关文件的行尾更改为 Unix 样式 (LF)。 The below files, located in the directory in which conda was installed, must to be converted and there may be others:以下文件位于安装 conda 的目录中,必须进行转换,可能还有其他文件:

    • etc/profile.d/conda.sh
    • Scripts/activate
    • Scripts/deactivate


    update : when conda updates itself the above files are overwritten at least some of the time, restoring the CRLF line endings so the process of the converting them to LF will have to be repeated under those circumstances.更新:当 conda 更新自身时,上述文件至少有时会被覆盖,恢复 CRLF 行结尾,因此在这些情况下必须重复将它们转换为 LF 的过程。

由于 Cygwin 模拟 linux 环境,我们需要使用“source activate test_env”而不是“activate test_env”。

one way to work with an env conda activated and cygwin is:使用 env conda 激活和 cygwin 的一种方法是:

  • open cmd: Win+R and write cmd打开cmd:Win+R并写入cmd
  • activate conda: conda activate env激活畅达:畅达激活环境
  • open cygwin: cygwin (for this cygwin must be added to the PATH)打开cygwin:cygwin(对于这个cygwin必须添加到PATH中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM