在Windows上与Cygwin一起使用anaconda环境 [英] Using anaconda environments with cygwin on windows

查看:621
本文介绍了在Windows上与Cygwin一起使用anaconda环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过Windows NT上的cygwin界面使用anaconda设置环境,但失败.

Trying to set up environments with anaconda through the cygwin interface on Windows NT, and failing.

创建环境(conda create -n test_env)可以正常工作.但是activate test_env失败.

Creating environments (conda create -n test_env) works fine. But activate test_env fails.

我曾尝试通过以下方式对其进行入侵:

I tried hacking it with:

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

这可以修复某些行为(which python指向正确的python).但是,如果我随后执行"conda install"命令,它将安装在根anaconda目录中,而不是环境中.也许导出对于bash会话是本地的,并且conda调用了不同版本的PATH?有没有办法对PATH进行全局修改?

This fixes some behavior (which python points to the right python). But if I then do a "conda install" command, it installs into the root anaconda directory, not the environment. Perhaps the export is local to the bash session, and conda calls a different version of PATH? There a way to make the modification of PATH global?

推荐答案

在解决了一段时间后,我认为我已经实现了一种合理且可行的方法,可以将Anaconda的python(及相关环境)集成到Cygwin中.假设您同时使用Cygwin和Anaconda来访问Cygwin的所有Anaconda工具,则.bash_profile中的以下设置似乎可以解决问题. (我只包括了与集成相关的.bash_profile部分...希望我不会无意间错过任何事情.)

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. 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. (I have only included those portions of .bash_profile relevant to the integration... hoping I did not miss something inadvertently.)

此设置实质上执行三件事.首先,用户需要明确地将目录$CONDA_BASE_DIR设置为conda/anaconda/miniconda基础环境的安装位置.其次,.bash_profile中具有使用外壳程序变量$CONDA_DEFAULT_ENV跟踪当前conda环境的功能.最后,我们定义别名cyg-conda和函数cyg-activate用作标准condaactivate命令的替换命令. 请注意,变量名称$CONDA_DEFAULT_ENV是特殊的,并且由实际的conda命令在内部使用.

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. Second, there is a functionality in .bash_profile to keep track of the current conda environment using a shell variable $CONDA_DEFAULT_ENV. 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. Please note that the variable name $CONDA_DEFAULT_ENV is special, and used internally by the actual conda command.

使用此设置,我可以像在Anaconda命令提示符下通常使用condaactivate的相同方式使用cyg-condacyg-activate,同时使环境可用于Cygwin bash shell

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.

肯定会接受改进建议等.

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

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

这篇关于在Windows上与Cygwin一起使用anaconda环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆