How and why you should use conda environments

I recently received a distress email from a buddy of mine, who is fairly wet behind the ears when it comes to python. He was having an issue getting a package on GitHub to run on his computer, because it was written for python 2.7, and he had installed python3 on his computer with an anaconda installation. So, obviously, my good friend hasn’t been introduced to the wonderful world of conda environments. Now, if you are scratching your head right now, because you don’t know what I’m talking about, or, maybe, you heard about these mystical unicorn like environments, but still don’t know if they are for you — just stick with me.

Basically, creating environments with conda is the perfect way to keep your main python installation on your computer clean, avoiding any type of conflicts that might arise due installing different package versions, because certain packages require certain package versions. So, anytime that you need to start a new project, which requires you to install new packages, then please think about this post. Also, if you are forking a GitHub repo and it requires special packages, as is the case with my friend, just say to yourself “conda environments”.

conda environments,  here we come. So, the first place to look is at the official documentation at http://conda.pydata.org/docs/using/index.html. Here you will find out everything you need to know, and probably a little bit more.

But in a nutshell, here’s is what to do. Go to your command line and create a new environment wit the following line of code:

$: conda create --name myenv python=2.7

 

Now, this line of code will create a basic python2.7 installation on your computer. It will be saved in the anaconda installation directory, which for me is in the home directory: ~/anaconda/envs/myenv.

In order to use it, you will follow the directions at the end of the installation, which tell you how to activate and deactivate your environment:

#to activate
$: source activate myenv
# the command line will change to reflect this by showing your env name in parenthesis.
(myenv) $:

 

In order to deactivate your environment, you would do the following:

(myenv) $: source deactivate
$:

 

So, once your environment is up and running, just activate it, and start installing what ever you need to use for your new project. Actually, if you know what you need, you can input this as an arguement at the time that you create the environment.

$: conda create --name myenv python=2.7 important-package

 

For more information on creating a conda environment which comes with a python data science development stack, please have a look at my other blog post: https://python-wrangler.com/accessing-your-python-data-science-stack-on-the-remote-host-with-jupyter-notebooks/

Okay, that’s enough for today. I think this was enough to answer my friend’s question, and get him back to work. Let me know if this was useful, or tell me how you use conda environments for your particular use-case in the comments section below.