22

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?

nick_eu
  • 3,541
  • 5
  • 24
  • 38

5 Answers5

27

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

###############################################################################
Patrick Kelly
  • 1,301
  • 1
  • 16
  • 25
  • Is set -o igncr needed if i'm running ZSH? – Luke Jun 19 '18 at 17:42
  • Somethings to remember for this integration to work. (i) Please install Anaconda directly from installer and not from package manager like Chocolatey, since for this approach to work the envs must be within the anaconda root directory. (ii) In order for anaconda python to work in cygwin commandline, you must use `python -i`. Using just `python` freezes the screen. – rambalachandran Oct 16 '18 at 05:59
  • Just to make things a bit more clear you need to `source` this script (either manually or in .bashrc), then run `cyg-activate` and then `source activate` your env. – FHTMitchell Nov 06 '18 at 16:29
  • This was very helpful and works. To make things run even more smoothly, I also added [the following function](https://pastebin.com/apgExv8n) to override the `conda` command and make things run a little more smoothly. – Zaya Nov 28 '22 at 01:26
12

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.

Grant Humphries
  • 2,696
  • 2
  • 23
  • 24
  • Item 1. of this answer solved all my Anaconda/Cygwin problems. It seems that there are many scripts with dos line endings. Even when I did "conda init bash" my .bash_profile was automatically converted to dos ending after adding the "eval" command, which broke the bash startup. See also https://stackoverflow.com/a/14607651/2264936 – chiefenne Jul 08 '19 at 20:08
  • This worked for me with a recent miniconda (4.7.10). Previously I had quite some Cygwin woes, but remarkably just setting `igncr` seems to have fixed everything. I think some other minor bugs have been fixed since I last tried. – Iguananaut Sep 03 '19 at 13:04
  • When using suggestion #1, `conda activate` dosn't activate the env, but `source activate` does – Aditya Kendre Nov 25 '20 at 21:48
  • After running `conda init bash`, I also had to copy the contents of `C:\Users\\.bash_profile` to the cygwin home directory `C:\cygwin64\home\`. Apparently it uses the windows `%USERPROFILE%` directory instead of the bash `$HOME` – goweon Nov 22 '21 at 21:49
3

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

siva008
  • 99
  • 6
  • 1
    Unfortunately that throws it's own errors. From the anaconda forums, the answer seems to be "wait for 4.1 or use the development version" -- it's fixed there. :) – nick_eu May 02 '16 at 15:44
  • Specifically, I get `bash: /usr/bin/conda: No such file or directory` – nick_eu May 02 '16 at 16:00
2

The answers to this question generally date to 2016. In 2022 things are different. I have a script in my $HOME/bin directory, I call it conda_stuff, which I source with . conda_stuff (I don't put it in my .bashrc as it slows the starting up of a terminal tab.)

Where anaconda is installed in C:\Users\john\anaconda3:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
eval "$('/cygdrive/c/Users/john/anaconda3/Scripts/conda.exe' 'shell.bash' 'hook')"
# <<< conda initialize <<<

And from here, we simply do e.g.

conda create -y --name py39a python=3.9
conda activate py39a # we're using this environment mainly
John Allsup
  • 1,072
  • 8
  • 9
  • Thanks. This (mostly - autocomplete generally isn't working) works for me . Anyone trying to do this probably already knows this but just in case (perhaps someone, like me, who usually works in either Linux *or* Windows may, like me, forget about this) - the script returned by conda.exe contains carriage returns (/r's). So you need to either make sure that you've got Cygwin set to ignore them or filter them out with like 'sed' inside the 'eval'. – notMyScreenName Jan 05 '23 at 21:49
1

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)