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

2022-01-10 00:00:00 python 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 中有一个功能,可以使用 shell 变量 $CONDA_DEFAULT_ENV 来跟踪当前的 conda 环境.最后,我们定义一个别名 cyg-conda 和一个函数 cyg-activate 用作标准 conda 的替换命令>激活命令.请注意,变量名 $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.

使用此设置,我可以像通常使用 conda 一样使用 cyg-condacyg-activateactivate 在 Anaconda 命令提示符处,同时使环境可用于我的 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

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

相关文章