Conda Environments

No more tears! If you are a user of conda packages, more precisely Jupyter Notebook, as I am, you should worry - a lot! - with your programming environment and your OS. Your environment is one of the barriers that prevent you from cloging your system with all sorts of packages and their - conflicting - versions. Lets be honest, you don’t want a crucial package being overwritten on your system do you?

So let’s go!

Creating an environment from ground up

$ conda create --name myenv

It is recommended to create your environment already with the packages you want to install, so there is no conflict issues with dependencies. This way

$ conda create --name myenv python scipy=0.15.0 astroid babel

Or

$ conda create --name myenv
$ conda install -n myenv python scipy=0.15.0 astroid babel

Note that:

  • Use of package_name=version to identify which specific version you want;
  • Creating environments this way loads default packages and if you want a clean environment you should use:
$ conda create --no-default-packages -n myenv _packages you want to install_

Creating an environment from an Yaml file

To create an environment from an .yml|yaml file, it has to have a specific structure, since the routines from the CLI will look for particular attributes to name and install dependencies through channels.

name: myenv
channels:
  - channel_name
dependencies:
  - python
  - scipy=0.15.0
  - astroid
  - babel

Awesome, now we do have a starting point: our env.yml. Lets go back to the terminal and:

$ conda env create -f env.yml

Respecting the env.yml path.

Updating an environment

There will be a moment when you will miss some dependency or another, versions are not up-to-date or even, there are better packages to work with. This time you will remember how ease pip is or even npm|yarn.

The best, documentation recomended, way is making a manual update on your env.yml file and execute the following:

$ conda env update --prefix ./env --file env.yml --prune

If you are a hasty person, and you cannot be without pip there is a way. But before that you need to config conda to use the python package manager.

$ conda config --set pip_interop_enabled True

Atention!

Only use pip to install new dependencies with your environment enabled!

$ pip install --upgrade-strategy only-if-needed package
$ pip freeze > requirements.txt

Or

$ conda list --explicit > requirements.txt

When its time to work

Like work had never came before eveything, doesn’t it?

$ conda activate

And to deactivate

$ conda deactivate

Useful Commands

# List Environment
$ conda env list

# Info
$ conda info --envs

# Map dependencies to a text file
$ conda list --explicit > requirements.txt

References

Conda Documentation


🍻
André