Mini-conda environment management, summarized!

September 29, 2019

Reading time ~2 minutes

Environment creation

  1. I can create a (simple) new environment.
$ conda create --name my_awesome_env_name

  1. I can create a new environment with a specific python version.
$ conda create --name my_awesome_env_name python=3.7.4

  1. I can create a new environment with
    • a specific python version.
    • a list of package to install.
$ conda create --name my_awesome_env_name python=3.7.4 pip jupyter myawesomepackage=1.x

  1. I can create an environment from a YAML file. Which is awesome as it allows us to share environment structure and dependencies across machines.

    Such file look like this:

    name: my_awesome_env_name
    channels:
    # A list of channels to use/look for package from can be given (another awesome feature :=) )
    - defaults
    - conda-forge
    dependencies:
    - python=3.7.4
    - pip
    - flask
    - pip: # All packages which are not available into conda's registry can be listed here.
        - python-dotenv
        - black

    And they can be used like this.

    $ conda env create --file path_to_the_awesome_yaml_file

Environment deletion

For the deletion is simple.

$ conda env remove --name my_awesome_env_name

Environment update

Sometime, we may need to update the environment.

As example:

  • We don't need an old package
  • A new version of a dependency is available
  • We new a new package

It's as simple as it's creation.

$ conda env update --file path_to_the_awesome_yaml_file --prune

The --prune argument is just the summon of environment management. Indeed it deletes all packages which we don't need :smile:

Environment list

I can get a list of all available environments.

$ conda env list

Package list

I can get a list of the installed package in an environment with the following.

$ conda list --name my_awesome_env_name

Environment clone

Just discovered with the help message that it's possible to clone an environment.

$ conda create --name my_awesome_clone_name --clone my_awesome_env_name

Environment file generation

It's possible to export the currently activated environment into a YAML file. It's practical as we can generate the file and share it.

$ conda env export > path_to_the_new_awesome_yaml_file_to_share

Environment activation

At any time we can switch from an environment to another. But the most important thing to remember: an environment can be activated like this:

$ conda activate my_awesome_env_name

Current environment name and location

To find the currently active environment, we can do this:

$ conda env list | awk '{ if (NF > 0 && substr($1,1,1) != "#" && $2 == "*") print $1 " " $3 }'

Environment deactivation

And to deactivate the currently active environment, it's even easier.

$ conda deactivate