Skip to content

Instantly share code, notes, and snippets.

@avoidik
Last active July 2, 2020 07:59
Show Gist options
  • Save avoidik/faad585417a315d037c7ae64a7afc5d1 to your computer and use it in GitHub Desktop.
Save avoidik/faad585417a315d037c7ae64a7afc5d1 to your computer and use it in GitHub Desktop.
Conda environments

How to manage Conda

In the example below we will install two same Python's versions side by side, but for the different architectures. When to use Conda? When you've been using pre-built Python packages. If you use pip or wheel packages a lot it is better to stick to vanilla Python distribution. That's it.

Preload Conda

Make sure you have the following either in ~/.bashrc or in ~/.bash_profile files defined

eval "$(/c/tools/miniconda/Scripts/conda shell.posix hook)"

or

source /c/tools/miniconda/etc/profile.d/conda.sh

or

# this will load base profile no matter what settings you have
source /c/tools/miniconda/Scripts/activate

To add Conda hook to Windows command-prompt (this will also enable UTF-8 charset)

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor]
"Autorun"="@chcp 65001>nul & call \"C:\\Tools\\miniconda\\condabin\\conda_hook.bat\""

To add Conda hook to Windows PowerShell

if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
$CondaHook = @'
& C:\Tools\miniconda\shell\condabin\conda-hook.ps1
'@
Add-Content -Path $PROFILE -Value $CondaHook

Ad-hoc run Windows PowerShell

powershell.exe -NoExit -ExecutionPolicy Bypass -Command "& C:\Tools\miniconda\shell\condabin\conda-hook.ps1"

To run Conda on Cygwin create new environment variable SHELLOPTS and set its value to igncr, then restart your shell.

rundll32.exe sysdm.cpl,EditEnvironmentVariables

Python

  1. Base configuration

    conda update -n base -c defaults conda
    conda config --set changeps1 false
    conda config --set auto_activate_base false # same as export CONDA_AUTO_ACTIVATE_BASE="false"
    conda config --set auto_update_conda false
  2. Create Python 3.6 x86 environment

    conda create -n python36_x86
    conda activate python36_x86
    conda config --env --set subdir win-32
    conda install python=3.6
    # alternative
    conda create -n python36_x86
    conda install -n python36_x86 -c defaults/win-32 --override-channels python=3.6
    # alternative
    conda create -n python36_x86 -c defaults/win-32 --override-channels python=3.6
    # now activate
    conda activate python36_x86
    # alternative
    conda env create --prefix ./envs -c defaults/win-32 --override-channels python=3.6
    conda activate ./envs
  3. Create Python 3.6 x64 environment

    conda create -n python36_x64
    conda activate python36_x64
    conda config --env --set subdir win-64
    conda install python=3.6
    # alternative
    conda create -n python36_x64
    conda install -n python36_x64 -c defaults/win-64 --override-channels python=3.6
    # alternative
    conda create -n python36_x64 -c defaults/win-64 --override-channels python=3.6
    # now activate
    conda activate python36_x64
    # alternative
    conda env create --prefix ./envs -c defaults/win-64 --override-channels python=3.6
    conda activate ./envs
  4. Manage packages

    # list packages
    conda list -n python36_x64
    conda list
    # search packages
    conda search requests
    conda search --channel defaults/win-64 --override-channels requests
    # install packages
    conda install --name python36_x64 requests
    conda install requests
    conda install -c conda-forge/win-64 --override-channels awscli boto3
    # update packages
    conda update conda
    conda update anaconda
    conda update python
    # remove packages
    conda remove -n python36_x64 requests
    conda remove requests
    # configure new environments to install packages by default
    conda config --add create_default_packages awscli
    # override with --no-default-packages flag
    conda create -n python36_x64 -c defaults/win-64 --override-channels --no-default-packages python=3.6
  5. Update all

    conda update --all --yes
    # except python
    conda install python=3.6.10
  6. Delete environment if not needed

    conda env remove -n python36_x86 -y
    # alternative
    conda remove -n python36_x86 --all -y
  7. Prepared environment definition

    # create & delete
    conda env export -n python36_x64 -c defaults/win-64 --override-channels > environment.yml
    # same as previous
    conda env export -n python36_x64 -c defaults/win-64 --override-channels -f environment.yml
    # now remove
    conda remove -n python36_x64 --all -y
    # restore
    conda env create -f environment.yml -n python36_x64 -p ./envs
    # update
    conda env update -n python36_x64 --file environment.yml --prune

Additional info

https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html

https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-pkgs.html

https://conda.io/projects/conda/en/latest/user-guide/configuration/use-condarc.html

https://docs.conda.io/projects/conda/en/latest/configuration.html

###############################################################################
# Anaconda Environment Selection - Plese set CONDA_BASE_DIR to the directory
# containing the base installation of anaconda/miniconda.
#
# Append to .bash_profile like so: source "${HOME}/.config/conda/activate.sh"
#
# For cygwin only
export CONDA_BASE_DIR="/cygdrive/c/tools/miniconda"
# 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 (base)
# 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 base
# 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}" != 'base' ]; 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='base'
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 "${HOME}/.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
###############################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment