Using anaconda environments with cygwin on windows

21,455

Solution 1

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

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

Solution 2

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

    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. The below files, located in the directory in which conda was installed, must to be converted and there may be others:

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

Solution 3

Since Cygwin emulates linux environment, we need to use "source activate test_env" instead of "activate test_env".

Solution 4

one way to work with an env conda activated and cygwin is:

  • open cmd: Win+R and write cmd
  • activate conda: conda activate env
  • open cygwin: cygwin (for this cygwin must be added to the PATH)
Share:
21,455
nick_eu
Author by

nick_eu

Just a social scientist trying to find his way through the world of applied data analysis!

Updated on July 09, 2022

Comments

  • nick_eu
    nick_eu almost 2 years

    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?