Last Updated:

Get started with Anaconda in Python

In this article, we'll install Anaconda, manage python packages, create separate conda environments, and share them via the conda YAML file. We'll cover most of these topics in the following order:

  • Installing Anaconda
  • Creating an Environment with Conda
  • Package Management via Conda
  • Saving and Loading the Environment
  • Enumerating, Sharing, and Deleting Environments
  • Best Practices
  • First of all, What is Anaconda and Why should I know about it?

You've probably already installed Python and will wonder why you need it at all. First, since Anaconda comes with a bunch of datascience packages, you'll be ready to get started with data. Second, using conda to manage your packages and environments will reduce future problems associated with the various libraries you'll be using.

Most real-world data science projects make extensive use of conda-based packages and environments, and I personally prefer installing and maintaining conda-based packages and then installing and maintaining the PIP-based packages themselves.

Anaconda is a distribution of packages created for datascience. It comes with conda, a package, and an environment manager. We typically used conda to create environments for isolating our projects using different versions of Python and/or different versions of packages.

We also use it to install, uninstall, and update packages in our design environments. When you download Anaconda for the first time, it comes with conda, Python, and over 150 scientific packages and their dependencies.

Anaconda is a pretty big download (~500 MB) because it comes with the most common datascience packages in Python, for people who are conservative about disk space, there's also Miniconda, a smaller distribution that only includes conda and Python.

You can still install any of the available packages using conda, which by default comes with the standard version. Conda is a program that we will use exclusively from the command line.

Installing Anaconda

Anaconda is available for Windows, Mac OS X, and Linux. You can find installation instructions at https://www.continuum.io/downloads. If you already have Python installed on your computer, it won't break anything. Instead, the default Python used by your scripts and programs will be the one that comes with Anaconda.

Choose Python version 3.5, you can install Python 2 version later. Also, select the 64-bit installer if you have a 64-bit operating system, otherwise use the 32-bit installer.

Once installed, you are automatically taken to the default conda environment with all the packages installed, which you can see below. You can check your own installation by entering the conda list into your terminal.

Creating Environments with Conda

You can use Conda to create environments to isolate your projects. To create an environment, use conda create -n env_namelist of packages in your terminal. Here, the -n env_name specifies the name of your environment (-n for the name), and listofpackages is the list of packages you want to install in that environment. For example, to create an environment named my_env and install numpy in it, type:

pen created in my_envnumpy

When you create an environment, you can specify which version of Python to install in that environment. This is useful when you're working with code in both Python 2.x and Python 3.x. To create an environment with a specific version of Python, do something like:

conda create-n py3 python=3

or

conda create -n py2 python=2

To install a specific version, use condacreate -n pypython=3.3 for Python 3.3. Once you have created the environment, use source activate my_env to enter it into OSX/Linux. In Windows, use:

activatemy_env

When you are in the environment, you will see the name of the environment in the terminal prompt. Something like:

(my_env) ~ $

By default, only a few packages are installed in your environment, plus the packages you installed when you created it. You can verify this with the condalist command.

Installing packages in the environment is the same as we saw earlier:

condainstallpackage_name

Only this time, the specific packages you install will only be available when you're in the environment. To exit the environment, type in OSX/Linux:

sourcedeactivate

On Windows:

deactivate

Package Management via Conda

Once Anaconda is installed, package management becomes fairly straightforward. To install the package, type:

condainstallpackage_name

For example, to install numpy, type:

condainstallnumpy

Save and load the environment

You can install multiple packages at the same time. Something like:

conda install numpyscipy pandas

This command will install all of these packages at the same time. You can also specify which version of the package you want by adding a version number, for example:

condainstallnumpy=1.10

Conda also automatically installs dependencies for you. For example, the scipy package depends on numpy because it uses and requires numpy. So, if you only install scipy conda, install scipy Conda will also install numpy if it is not already installed.

Most commands are pretty intuitive. To uninstall, use:

condaremovepackage_name

To update a package:

condaupdatepackage_name

If you want to update all the packages in your environment, which is often useful, use:

condaupdate —all

Finally, to list the installed packages:

listconda

If you don't know the exact name of the package you're looking for, you can try searching with:

condasearchsearch_term

For example, if you want to install a package that reads and writes excel files, but are not sure of the exact name of the package.you can try searching for the keyword excel:

condasearchexcel

It returns a list of available excelwriter packages with the appropriate package name, which I personally recommend, XlsxWriter.

Save, enumerate, share, and delete environments

Save and share environments

A really useful feature is sharing environments so that others can install all the packages used in your code with the correct versions. You can save packages to a YAML file using condaenvexport>environment.yaml

The first part records all the packages in the environment, including the Python version. Above you can see the name of the environment and all the dependencies (along with the versions) are listed. The second part of the export command >environment.yaml
writes the exported text to the YAML file environment.yaml. You can now share this file, and others can create the same environment that you used for the project.

To create an environment from an environment file, use:

condaenv create-f environment.yaml

This will create a new environment with the same name specified in environment.yaml.

Enumeration of Environments

If you've forgotten what your environments are called (this sometimes happens to me), use condaenvlist
to list all the environments you've created. You should see a list of environments, next to the environment you are currently in, there will be an asterisk. The default environment used when you're not in it is called the root environment.

Deleting Environments

If there are environments that you no longer use, you can remove them using condaenv remove-n env_name