Conda notes

From raju

replicate environment

Method 1

Check the environments installed

    conda info --envs
    

Export the environment

    conda env export --name FOO | grep -v "^prefix: " > environment.yml
    

Note: By default, the output of "conda env export" contains a line

    prefix: /path/to/the/environment
    

This line is not necessary in order to recreate the environment. Grepping it out so that the replicating user will not know what the original environment's path is.

Another way

  • On Windows
    activate myenv
    conda env export | grep -v "^prefix: " > environment.yml
    deactivate
    
  • On Linux
    source activate myenv
    conda env export | grep -v "^prefix: " > environment.yml
    source deactivate
    


To recreate it

    conda env create -f environment.yml
    activate myenv
    

Ref:-


Method 2

Get the list of packages

    $ conda list --export --name old_environment > req.txt
    

or

    $ conda list -e -n old_environment > req.txt
    

Create a new environment using

    $ conda create -n new_environment --file req.txt
    

dummy

List all the environments

    conda info --envs
    

clone an environment

tags | copy environment

    conda create --name new_environment --clone old_environment
    

Ref:- https://conda.io/docs/user-guide/tasks/manage-environments.html#cloning-an-environment

Create an environment

Always specify the python version you want while creating the environment. For example

    conda create --name foo python=2.7
    

If python version is not specified, there will not be a python.exe under the corresponding environment directory /c/ProgramData/Continuum/Anaconda/envs/foo . The python.exe is needed to link this environment in pycharm when choosing the python interpreter path.

Sample commands used in the past

    conda create -n market_data_processor python=3.6 pandas pep8 pandas-datareader
    

Create an environment for python 3.6

    conda create -n py36 python=3.6 anaconda
    

Switch to the new environment

    activate py36
    

Remove an environment

    conda env remove --name foo
    

tags | delete

list current environment

Use $CONDA_DEFAULT_ENV to get the current environment name. For example

    $ source activate py36
    $ echo $CONDA_DEFAULT_ENV
    py36
    

This is useful to activate a conda environment if it is not the current environment. See for example https://github.com/KamarajuKusumanchi/rutils/blob/master/bin/windows/python3

install a specific version of a package in an environment

    conda install -n <env> <package>=<version>
    

For example

    conda install -n py27 pandas=0.19.2
    

find the package version

To find the version of the package installed

    conda list -n env_name package_name
    

For example

    conda list -n foo pandas
    

You can use this technique to check if a package is installed.

Other related commands

  • conda list - list all packages in the current environment

Ref:- https://conda.io/docs/commands/conda-list.html

path to python

The python used by an activated conda environment is ${CONDA_PREFIX}/bin/python

activate a conda environement if it is not already activated

    current_env=$CONDA_DEFAULT_ENV
    # desired environment is set to py36. Change it as needed.
    desired_env=py36
    
    if [[ $current_env != $desired_env ]];
    then
        echo "setting environment to $desired_env"
        source activate $desired_env
    fi
    

In action | https://github.com/KamarajuKusumanchi/rutils/blob/master/bin/windows/python3

specify minimum version in the environment file

https://conda.io/docs/_downloads/conda-cheatsheet.pdf -> pg-2 -> "Specifying version numbers" section

update environment

    conda env update -f FILE
    

where FILE is the path of the environment file

Ref:- https://conda.io/docs/commands/env/conda-env-update.html

sample environment file

    name: py36
    channels:
      - anaconda-fusion
      - defaults
    dependencies:
      - python=3.6
    

useful links

file locations

  • pep8 - /c/ProgramData/Anaconda3/Scripts/pep8.exe
  • python interpreter - /c/ProgramData/Anaconda3/python