8 min read

Getting Started with Anaconda

1 Introduction

If you want to work with Python you need a suitable IDE.

IDE stands for “Integrated Development Environment” and there are many different ones you can use for Python:

to name just a few.

I personally prefer to use Jupyter Notebooks. For this we need Anaconda. Anaconda is a free and open source distribution of the programming languages Python and R for scientific computing (among others: data science, machine learning).

Using Anaconda in conjunction with Jupyter Notebooks offers two distinct advantages:

  • Using Jupyter Notebooks, you can create and share documents that include live code, equations, visualizations, and narrative text.
  • Using Anaconda, you can easily and quickly create virtual environments, extend them as desired and share them with other colleagues or clients.

In this post I want to show how to get Anaconda and Jupyter Notebooks running.

2 Install Anaconda

First of all we have to install Anaconda. You can do this from here: Anaconda Installation.

Here you can select for which operating system you want to have Anaconda installed. For Windows for example follow this link: Anaconda Installation for Windows.

After the .exe file has been downloaded, run it and click through the installation. There is only one step to pay attention to here. When you get to the Advanced Options section, select ‘Add Anaconda3 to my PATH environment variable’. Otherwise we would have to add this path manually later.

3 Getting Started with Anaconda Navigator

After successful installation, search for Anaconda Navigator on your computer.

As we can see, different IDEs have already been installed with it, including Jupyter Notebooks. On the left side we can now select Environments.

What are environments?

To program with Python we need different libraries (for example numpy or pandas). We can store these collected in virtual environments. It is of great advantage that you can also use specific versions of the libraries.

To program with Python we need different libraries (for example numpy or pandas). We can store these collected in virtual environments. It is of great advantage that you can also use specific versions of the libraries. Why is this important, especially in project work, when you have developed models or done analyses for customers? You have the option of extracting all the libraries used for this purpose with their respective version and handing them over to the customer. In this way, the customer is able to execute the code without errors for a long time. It often happens that parts of packages change and then no longer work. That’s why version management is so incredibly beneficial.

Create a new Environment

We have now selected Environments on the left side. At the bottom there is a button ‘Create’ with which you can create a new environment.
Give it a desired name and select the Python or R programming language.

Click on your new environment and select ‘Not installed’ at the top.

Now you can search for new libraries that you have not yet installed in the respective environment. Here for example Pandas.

At the bottom you can press ‘apply’ to perform the installation. Often it happens that with large libraries like pandas is one, further packages are installed with it.

In our demo_env I installed the following basic libraries:

  • pandas
  • numpy
  • matplotlib
  • seaborn
  • pip
  • scikit-learn
  • keras
  • tensorflow
  • xgboost
  • jupyter_contrib_nbextensions

Of course, some cool libraries will be added over time. How to add them you know now.

4 Getting Started with Jupyter Notebooks

With Anaconda, the ‘Anaconda Powershell Prompt’ was also downloaded. With its help I always start my Jupyter Notebooks.

4.1 Exploring your environments

With the following command we get an overview of the existing environments installed:

conda info --envs

To see which libraries with which version are installed in a particular environment use the following command:

conda list -n demo_env

4.2 Change your working direcotry

The first thing I always do is specify where I want to work. I can do that easily with the command cd "path/to/root directory

4.3 Activate your Environment

To activate your environment use: conda activate demo_env

The brackets at the beginning of the line now say (demo_env) inside. From this you can see which environment is currently active.

4.4 Start of the Jupyter Notebook

Quite easy just type: jupyter notebook

4.5 All in One

cd "path/to/root directory

conda activate demo_env

jupyter notebook

A new tab is generated in the specified default browser from which you can launch Jupyter Notebooks.

How exactly the Jupyter notebooks can be used is not central to this post. But you can find lots of beginners guides on the internet:

5 Anaconda Powershell Prompt

I actually manage my environment exclusively via the Anaconda Powershell Prompt. The reason for this is that I can’t do everything via the Anaconda Navigator. For example, I can’t find all libraries via the search function such as the very popular jupyter_contrib_nbextensions. Or extracting environments for a complete project handover.

5.1 Installing new libraries

When I want to add another library to my existing environment and I don’t know exactly what it’s called, I always search the internet like this: ‘Anaconda Cloud Install Library’.

Very quickly you will also find what you are looking for under Anaconda Cloud.

Here the page for example for the installation of Pandas:

Below is the exact name of the library.

I now use the Anaconda Powershell prompt again and first activate the environment in which I want to install the new package and then use ‘pip install’ to install the new library.

conda activate demo_env

pip install pandas

Update existing libraries

With this command you can not only install new libraries, you can also update them to the latest version.

If you want to update your complete environment you can do it with this:

conda update -n demo_env --all

5.2 Export environments

As mentioned before, it is not possible to export existing environments via the Anaconda Navigator. But via the Anaconda Powershell Prompt it works.

First I specify the path where I want to save the environment. Then I activate the environment I want to export. Then I execute the following command:

cd "path/to/root directory

conda activate demo_env

conda env export > environment.yml

The activated environment is now exported to the named destination and saved as a .yml file with the name ‘environment’. The name can of course be used/changed as desired.

Here it is

I always use Visual Studio Code to open this .yml file.

Change Character Encoding

We have only one problem here. By default the file is extracted and saved in UTF-16. So we can’t import it as a new environment. But there is a workaround for this. We open the .yml file in Visual Studio Code. On the bottom right we see the current character encoding.

Click on it and select ‘Reopen with Encoding’. Select UTF-8.

The file now looks very strange.

Type ctrl+z

Voilà

Now the file can be saved.

But first I change the name (at the top). I use the same as I named the .yml file so that it is unique.

The prefix at the end of the file can also be deleted.

5.3 Import environments

Importing environments can be done in two ways:

  • via Anaconda Powershell Prompt
  • via Anaconda Navigator

5.3.1 via Anaconda Powershell Prompt

Here I navigate again with cd to the respective place where the .yml file is stored. Then I execute the following command:

cd "path/to/root directory

conda env create -f environment.yml

5.3.2 via Anaconda Navigator

Let’s use the Anaconda Navigator again and go to the environments via the corresponding button on the left side. At the bottom there is a button called ‘Import’. With this we can import our new environment as well.

As we can see, our new environment is now also listed.

5.4 Updating packages/libs

To update a specific packages within an environment use:

conda activate demo_env

pip install --upgrade pandas

5.5 Removing packages/libs

To remove a specific packages within an environment use:

conda activate demo_env

pip uninstall pandas

6 Closing Words

You can view the saved libraries under the following path on your computer:

C:\Users\anaconda3\envs

If you delete these files, they will no longer appear in the Anaconda Navigator or in the Anaconda Powershell Prompt.

7 Download the created environment

If you want to have the created sample environment feel free to download it from my GitHub Repository.

Possibly the packages are no longer up to date. You should therefore update the most important libraries. Here is my list of pip commands, which I call in the Anaconda Powershell Prompt after I have selected the desired environment:

pip install pip
pip install pandas
pip install numpy
pip install matplotlib
pip install seaborn
pip install scikit-learn
pip install tensorflow
pip install keras
pip install nltk
pip install pmdarima