Predictive Hacks

# Basic Examples of Anaconda Environments

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?

Because 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

Most commonly, 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 install either 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*' With 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 foobar, and blob up to the latest compatible versions mutually satisfiable.

#### 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?

The 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 conda install).
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.:

#### 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 switch --name or -n allows you to query another environment. For example,

# 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

Replace 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 environmentname.yml.

### Extra Sources

You can also find a cheet sheet of Anaconda and also some good examples!

Python