This post is a gentle introduction about
Anaconda Environments which is like the “Docker” of the Machine Learning projects. It is very important when we are working on a project to be reproducible and for that reason, we want to be able to share our working environment with our colleagues, or each project to be in a different environment. Notice that
conda supports Python, R, Scala and Julia but we will focus on Python in this post.
How to get the Anaconda Distribution?
The open-source Anaconda Distribution is the easiest way to perform Python/R data science and machine learning on Linux, Windows, and Mac OS X. With over 15 million users worldwide, it is the industry standard for developing, testing, and training on a single machine. You can download the Anaconda Distribution from the official site.
How to get my Version of conda?
Run a command to determine what version of
conda you have installed.
$ conda -V #or $ conda --version
How to get all the installed packages?
conda installs packages automatically, it’s hard to know which package versions are actually on your system. That is, packages you didn’t install explicitly get installed for you to resolve another package’s dependencies. The following command lists all the installed packages.
$ conda list
How to install a specific version of a package?
conda allows you to install software versions in several flexible ways. Your most common pattern will probably be prefix notation, using semantic versioning. For example, you might want a MAJOR and MINOR version, but want
conda to select the most up-to-date PATCH version within that series. You could spell that as:
$ conda install foo-lib=12.3
Or similarly, you
may want a particular major version, and prefer
conda to select the latest
compatible MINOR version as well as PATCH level. You could spell that as:
$ conda install foo-lib=13
If you want to narrow the installation down to an exact PATCH level, you can specify that as well with:
$ conda install foo-lib=14.3.2
you’ll use prefix-notation to specify the package version(s) to install. But
conda offers even more
powerful comparison operations to narrow versions. For example, if you wish to
bar-lib versions 1.0, 1.4 or 1.4.1b2, but definitely
not version 1.1, 1.2 or 1.3, you could use:
$ conda install 'bar-lib=1.0|1.4*'
conda you can also use
inequality comparisons to select candidate versions (still resolving dependency
consistency). Maybe the bug above was fixed in 1.3.5, and you would like either
the latest version available (perhaps even 1.5 or 2.0 have come out), but still
avoiding versions 1.1 through 1.3.4. You could spell that as:
$ conda install 'bar-lib>1.3.4,<1.1'
How to Update a Package?
Note that this
conda command, as well as most others allow specification of multiple packages on the same line. For example, you might use the following command to bring all of
blob up to the latest compatible versions mutually satisfiable.
$ conda update foo bar blob
How to Remove a Package?
You can remove a package by typing for example following command:
$ conda remove pandas
How to Search for Packages?
# Examples: # Search for a specific package named 'scikit-learn': $ conda search scikit-learn # Search for packages containing 'scikit' in the package name: $ conda search scikit # Note that your shell may expand '*' before handing the command over to conda. # Therefore it is sometimes necessary to use single or double quotes around #the query. $ conda search 'scikit' conda search "*scikit" # Search for packages for 64-bit Linux (by default, packages for your current platform are shown): $ conda search numpy[subdir=linux-64] # Search for a specific version of a package: $ conda search 'numpy>=1.12' #Search for a package on a specific channel $ conda search conda-forge::numpy conda search 'numpy[channel=conda-forge, subdir=osx-64]'
How to find dependencies for a package version?
conda info command reports a variety of details about a specific package. The syntax for specifying just one version is a little bit complex, but prefix notation is allowed here (just as you would with
For example, running
conda info cytoolz=0.8.2 will report on all available package versions. As this package has been built for a variety of Python versions, a number of packages will be reported on. You can narrow your query further with, e.g.:
$ conda info cytoolz=0.8.2=py36_0 cytoolz 0.8.2 py36_0 <hr />----------------- file name : cytoolz-0.8.2-py36_0.tar.bz2 name : cytoolz version : 0.8.2 build string: py36_0 build number: 0 channel : https://repo.continuum.io/pkgs/free size : 352 KB arch : x86_64 constrains : () date : 2016-12-23 license : BSD md5 : cd6068b2389b1596147cc7218f0438fd platform : darwin subdir : osx-64 url : https://repo.continuum.io/pkgs/free/osx-64/cytoolz-0.8.2-py36_0.tar.bz2 dependencies: python 3.6* toolz >=0.8.0
You may use the
* wildcard within the match pattern. This is often useful to match
'foo=1.2.3=py36*' because recent builds have attached the hash of the build at the end of the Python version string, making the exact match unpredictable.
Determine the dependencies of the package
numpy 1.13.1 with
Python 3.6.0 on your current platform.
conda info with specifiers for
numpy version and Python version.
How to get a list of the Environments?
With the following command, you can get the list of the conda environments and the currently activated one is marked with an asterisk in the middle column.
$ conda env list
How to get the installed packages in an environment?
It is often
useful to query a different environment’s
configuration (i.e., as opposed to the currently active environment). You might
do this simply to verify the package versions in that environment that you need
for a given project. Or you may wish to find out what versions you or a
colleague used in some prior project (developed in that other environment). The
-n allows you to query another
environment. For example,
$ conda list --name test-env 'numpy|pandas'
How to switch between environments?
To deactivate an environment and deactivate, you type the following commands:
$ conda activate ENVANME $ conda deactivate # it returns you to the root/base environmnet
How to remove an environment?
The command to remove an environment is:
$ conda env remove --name ENVNAME # or $ conda env remove -n ENVNAME
How to create a new environment?
You can create a new environment with the following command:
$ conda create --name myenvname
Or if you want also to specify the Python version:
$ conda create -n myenvname python=3.7.4
and you can switch to this environment by typing:
$ conda activate myenvname
How to clone an environment
Use the terminal or an Anaconda Prompt for the following steps:
You can make an exact copy of an environment by creating a clone of it:
conda create --name myclone --clone myenv
myclone with the name of the new environment. Replace
myenv with the name of the existing environment that you want to copy.
To verify that the copy was made:
conda info --envs
How to export an environment?
Usually, we export the environments to a yml file using the following command:
For example, export the environment called
myenvname to the file
$ conda env export --name myenvname --file environmentname.yml
How to create an environment from a shared specification?
To create an environment from
environmentname.yml, you can use the following command:
$ conda env create --file environmentname.yml