Working With Python Environments, Anaconda Package Manager and IDEs

Darshan Patel
DataReply
Published in
5 min readMay 2, 2018

With packages being the core of everyday projects, it is important to bear in mind that not all python versions support every package. Additionally, transitioning from python 2 to 3 might be difficult due to dependencies. Matters could worsen if you have both versions installed in your root directory. This is where environments come in. By assigning projects to separate environments, which each have their own python version, you solve this issue entirely. This is important for an up and coming data scientist or anyone else that might be using python for their day to day work. We all want to avoid something like this:

Credits to: https://m.xkcd.com/1987/

In this blog I will go over how to use anaconda package manager to assign projects to separate environments and install packages within them.

Things to keep in mind

  1. Do not use sudo to install random python packages
  2. Do not install any/all python packages you need to root
  3. Rule of thumb : use a different environment for every python project you work on.

Setup:

  1. Download Anaconda Package Manager
  2. Download and install an IDE of your choice (I recommend PyCharm and will use it through out this guide)

Step 1: Create a new enviroment and link it to your project

There are 2 ways of doing this

1.Create a new Anaconda Environment using Terminal then use that environment when setting up a new project in PyCharm

To create a new environment from terminal use the following command:

conda create --name nameofyourEnvironment

Or to create a new enviroment with specific python version use

conda create -n nameofyourEnviroment python=3.6

More ways of creating environments can be found here.

Once the environment has been created, you can open up the IDE of your choice and select the environment you just created.

For PyCharm this can be done by selecting File >> New Project. In the new project window click the Existing Interpreter radio button (1) and then select browse (2)(see annotated image below).

In the python interpreter window select Conda Environment then on the dropdown on the right choose the python environment you just created and then click ok (see image below).

2. Create an environment from the IDE when you create a new python project

PyCharm allows you to create an Anaconda Environment when creating a new project

To do this click on create a new project and then on the new project screen click the new environment using: radio button (1) and select conda from the dropdown (2) (see annotated image below).

It is worth noting that for the environment path (in this case: /anaconda3/envs/mediumenvblog) the last part is the name of the environment. This will be used later to install packages to the project enviroment.

Step 2: Linking a new/existing enviroment to an existing project

Go to preferences or settings for your choice of IDE and locate the Project Interpreter section and then click Add a new iterpreter or anything similar. For PyCharm you can do this by going to Preferences and then clicking on Project: <current project name>(1) and then Project Interpreter (2). Then click the settings button next to the Project Interpreter dropdown and click add (3) (see annotated image below).

On the add python window you can either create a new anaconda environment or use an existing one in a similar fashion to Step 1.

Step 3: Installing packages to specific anaconda enviroments

For this step we will assume that the package we want to install is pandas and the enviroment we want to use is mediumblogenv.

To do this, we first navigate to the anaconda website here. Then in the search bar type in the package you want to install and click search.

You will be recieve a few results based on your search. Select and click on the result you want. I will be using the first option conda-forge / pandas.

Then just copy the corresponding conda code. In this case it is: conda install -c conda-forge pandas

Now open terminal and type in the following:

source activate theEnviromentYouWantToUse

you will know you are in the environment you want to use if the environment name appears at the front like this:

Once in the environment, you can install any package you want and see it along with its dependencies in your Project Interpreter section.

Here is a before and after screenshot of installing pandas on the mediumblogenv

Before on the left and After on the right

Pandas is now visible in the Project Interpreter section. I can now start using pandas for my project.

Extras 1: List existing environments

To list any existing environments copy the following command to terminal:

conda info --envs

Extras 2: Deleting an existing anaconda environment

To delete an existing conda environment copy the following command to terminal:

source deactivate theEnvironmentYouWantToDelete

Summary

In this blog we learned how to create and use different anaconda environments; either with a new project or an existing one. We also covered how to install packages to that specific environment. Note that this is one way among many to avoid conflicts within projects. Hopefully this blog post is helpful to new data scientists or python newcomers.

--

--