How to get started with Anaconda

Stefan Blos
6 min readApr 7, 2019

--

An Anaconda snake watching you read this article — Photo by David Clode on Unsplash

What is Anaconda and why do I need it?

When working with Python in different projects on a single system the problem of package and version management can get really frustrating. Some projects may require an older Python version like 2.7(you should definitely look to upgrade) compared to another one that needs 3.7. Maybe different packages depend on different versions of other packages and you break dependencies in places you don’t even know by updating packages.

In the beginning it may seem like an overhead to use a different environment for every project. After working in multiple projects though you might agree that there is definitely a great argument to be made for a tool that manages these environments.

First of all I do not say that Anaconda is the greatest tool for this or that it is perfect. There are many others out there like virtualenv, virtualenvwrapper, pyenv, etc. (this list actually goes on) and if you are better off using them this is perfectly fine. I noticed though that especially in the data science and machine learning space Anaconda seems to be the most frequently used solution.

What does it do?

So you are most probably familiar with using pip as a package manager. You simply install packages from the command line with pip install <package-name>. What this does is it adds the package to your default Python installation in a certain path on your machine (this path being: <Path-to-Python>/site-packages/ ).

Anaconda breaks this up by doing two things:

  • creating environments to isolate projects from each other
  • using conda install instead of pip

(You might think now: “But I love pip and it has been part of my life for so long I can’t just abandon it!” Be sure that this kind of affection to a package manager is not troublesome at all and that you will find the solution to all your worries in the later section Using pip inside of an environment.)

So let’s look at the basic workflow in detail and then some more neat little tipps and tricks after that.

Creating environments

After installing Anaconda from the wonderful guide on their website you can start an Anaconda prompt:

  • Windows: press Windows Key -> type Anaconda Prompt -> hit Enter
  • Mac: press CMD + SPACE -> type Terminal -> hit Enter

You can now check if everything is working fine by typing conda --version which displays the current version of Anaconda (for more info which might confuse you at the beginning you can type conda info). At the time of this writing it outputs conda 4.6.11 on my machine which may be higher for you now.

Great, now you are all set to create your first Anaconda environment. So we can create an environment with the command conda create -n <env-name>. In this example I’ll use an environment called test-env but you should name it something more useful, related to your project.

conda create -n test-env

This creates a new (isolated) environment with the default Python version. You can activate this now in the following way, depending on your conda version and machine:

  • version >= 4.6.: conda activate test-env
  • Windows: version < 4.6: activate test-env
  • Mac: version < 4.6: source activate test-env

What you will realize now is that your command prompt will feature the name of your environment in parenthesis at the start, e.g.:

(testEnv) C:\Work>

This is pretty convenient because you always know which environment you are currently in and don’t get confused. However you can also always list all environments you have created and have the currently active one marked with an asterisk (*). To do this, simply type conda env list.

If you want to exit the environment you can simply type the following command depending on your machine:

  • version >= 4.6.: conda deactivate
  • Windows: version < 4.6: deactivate
  • Mac: version < 4.6: source deactivate

You will notice that the name of your environment is not longer shown in parenthesis at the start of your command prompt.

I mentioned previously that you can manage different python versions in different environments. This can also be done really easy by specifying the version when you create an environment. If we want to create an environment where we use python version 2.7 we can type:

conda create -n <env-name> python=2.7

In comparison we can create an environment with version 3.7 with:

conda create -n <env-name python=3.7

This way you can easily switch between different versions of python in different projects and always ensure compatibility.

Finally if we want to delete an environment from our machine (including all the packages we installed inside of the environment) we can use

conda env remove --name <env-name>

Installing packages with conda

If you are used to pip then you will feel right at home because now you can simply install packages with conda using:

conda install <package-name>

This will check if the package is available and allows you to install the latest version to your machine. The cool thing is that it will only install it to the environment you are currently in. To list all packages in your current environment simply type conda list.

You may be unsure if a certain package is available in the Anaconda repository so you can search for available packages with conda search <package-name>. Then you can see if it is available and then install it if you like (if it is not available you might like to have a look at the later section Using pip inside of an environment).

It is also possible (and actually recommended) to install multiple packages in a single command with conda install <package-1> <package-2> <package-3>. So as an example if we want to start doing some data science we could install some basic packages like numpy, pandas and matplotlib like this:

conda install numpy pandas matplotlib

Sometimes of course newer version for packages will be available so we can simply use the command conda update <package-name> to update a single package (this also works for our Anaconda version with conda update conda).

Of course if a package is no longer needed we can also remove it easily by typing:

conda remove <package-name>

Using pip inside of an environment

I mentioned before that many people are used to install packages in python with pip. And in fact some packages are only available via pip. Although it is recommended to first try to install packages using conda we might not find a certain package we need (of course we first tried conda search <package-name to make sure it is not available). In this case it might be useful to use pip. But how does that work inside of our environment?

If only we had a tool to install things in our environment…Oh wait, conda does just that. We only need to install pip inside of our environment using conda install pip. If we then use pip install <package-name> it will install the package inside our current environment. This is super useful for packages that are non-existent in conda.

You might ask how that pip installation interferes with the basic one on your machine and the answer is straight-forward: if we are inside of a conda environment it will always try to use the pip version of the environment. If no pip version is present in the environment it will try to fall back to other ones installed on your machine. We can see this if we create a new environment and install pip inside of it. If we then type pip list we will see no packages installed (there might actually be a few that are preinstalled depending on your configuration.

Pro tip: Of course this means we always have to remember to install pip inside of our environments. There is a neat little trick to do this right when creating the environment and it is to simply append it to our creation command like this:

conda create -n <env-name> pip

This way a new environment is created and pip is directly installed so when we use it inside of the environment it automatically uses the correct version of pip (this is possible with every other package as well and even allows for multiple packages).

Summary

This article is intended to show you that it is really simple to create different environments for your different Python projects. It really is recommended to use a tool like this and Anaconda makes it easy for you to get started. There might be some confusion at the beginning but I hope you find this article helpful and can use it to understand the basic principles.

Of course there is a lot more to Anaconda and a lot of topics are not discussed here (but maybe in a later post) for the sake of simplicity. If you have problems with the described steps or questions / suggestions for future articles on more complex topics feel free to comment on this article or hit me up on Twitter @stefanjblos. Thanks for reading and have a great day!

--

--