Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using anaconda environments with cygwin on windows

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

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

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?

like image 474
nick_eu Avatar asked May 01 '16 17:05

nick_eu


People also ask

Can I use Anaconda on Windows?

This tutorial will demonstrate how you can install Anaconda, a powerful package manager, on Microsoft Windows. Anaconda is a package manager, an environment manager, and Python distribution that contains a collection of many open source packages.

Can I install Anaconda in a virtual environment?

It is meant to be its own virtual environment, more or less. It is meant to be installed in your home directory for the local user( ~/anaconda3 by default). From there, you will create the necessary virtual environments by creating separate conda environments (using the conda create... and conda activate... commands.


1 Answers

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.)

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.

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

###############################################################################
like image 81
Patrick Kelly Avatar answered Sep 29 '22 17:09

Patrick Kelly