Using anaconda environments with cygwin on windows
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 SHELLOPTSset -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.execyg-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_PREFIXPATH=$PATH:$CONDA_PREFIX/Library/mingw-w64/binPATH=$PATH:$CONDA_PREFIX/Library/usr/binPATH=$PATH:$CONDA_PREFIX/Library/binPATH=$PATH:$CONDA_PREFIX/ScriptsPATH=$PATH:$HOME/scriptsPATH=$PATH:$HOME/local/binPATH=$PATH:/usr/local/binPATH=$PATH:/usr/binexport PATH###############################################################################
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:
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 SHELLOPTSfi
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.