Installation using Conda

To create a conda environment named ‘myenv’:

conda create --name myenv

To create an environment from a file ‘test.yml’:

conda env create -f test.yml

The environment name comes from the line ‘name: tag’ inside the ‘test.yml’ file.

To create a named environment from a file ‘test.yml’:

conda env  create -f test.yml -n pytorch

To create an environment from the base environment:

conda create --name myenv --clone base

To remove an environment named ‘envname’:

conda env remove -n envname

List all conda environments:

conda env list 

To export a conda environment from within the environment to a file ‘test.yml’:

conda env export > test.yml

To export a specific environment, in this case just the base environment (usually called ‘base’):

conda env export -n base > requirements.txt

To export only the modules that were requested:

conda env export --from-history > test.yml

This allows you to install modules that were installed with pip as well. You can do this by modifying the file and adding the pip modules as shown:

name: rq_env
channels:
  - defaults
  - conda-forge
dependencies:
  - rq
  - pyarrow
  - jsonpickle
  - pandas
  - oauthlib
  - openssl
  - pytables
  - tweepy
  - numpy
  - requests
  - redis
  - pip
  - pip:
    - rq-scheduler

To create a named environment ‘pytorch’ from a file ‘requirements.txt’:

conda env create -f requirements.txt -n pytorch

To update a conda environment named ‘test’ from a file ‘requirements.txt’:

conda env update -n test -f requirements.txt

This has to be done from outside the environment after the environment has been deactivated.

To list the changes that you have made to your environment

conda list --revisions

To revert back to a previous version use:

conda install --revision=REVNUM

where REVNUM is the revision number obtained from listing the revisions

Installation from Pip ‘requirements.txt’ file

If you need to install from a pip ‘requirements.txt’ file, first create an empty conda environment, install pip inside it and then proceed to install the packages using pip inside it. The following code illustrates installation from a file. The ‘-I’ flag ignores an existing installation, if you need to remove that version and install the one from the ‘requirements.txt’ file you have to manually delete the package from the current conda environment.

conda create -n testenv 
source activate testenv 

If you do not have pip inside your conda environment, invoking pip defaults to the global pip module. As a result the packages will not be installed into your environment.

conda install pip
pip install -I -r requirements.txt 

It is a good idea to check that your package has been installed using

pip freeze

Explicitly list packages in an environment file

If you want to match the exact packages, use this however keep in mind that will not translate across platforms

conda list --explicit > spec-file.txt

Create the environment using this

conda create --name myenv --file spec-file.txt

A note on installing Jupyter notebooks

Make sure that you have the jupyter module inside your environment. Similar to the issue with pip, if none is found in the environment a global Jupyter module will be used resulting in the notebook spawned having no awareness of the environment you just created. A good check to make sure that your notebook is using the right command is the following commands

!echo CONDA_DEFAULT_ENV

The above will usually return the expected environment but that does not mean the enviroment and its modules are available (unfortunately!)

import sys
print(sys.executable)

The above should return a python executable that points to your environment path, if it is referencing the system path or a global path for the python executable you do not have the environment available in your notebook and load a module from your environment will fail.