Conda > Other Python Virtual Envs, Prove Me Wrong

When I first started learning Python, I wished that someone would have forced me to use a virtual environment manager. I started out using conda when I started my MOOC for Udacity, but never took advantage of the capabilities of its package management system. Every package I needed to install was installed to the base environment. I got a bit smarter and started using virtualenv. I then moved to virtualenvwrapper, and then figured out that conda was the best all along. 

Why conda? No need to modify a .bashrc file, install pip or virtualenv separately, or add extra commands like pipenv when you want to run a python script.

Just to clarify, this post will help you install Anaconda. This is conda + python + many useful data science packages. If you don’t need the data science packages, you can just install miniconda.

In this post, I’ll show you how to setup your first python development environment with conda.

Installing Conda

Installing anaconda is a breeze. You can download it here. Download the 32 or 64 bit installer depending on your system. Do not install python 2.7 unless you have a good reason for it. Python 2.7 won’t be supported past 2020.

 

As you go through the installation, you will get to this page:

Adding to your path is something that depends on your needs. If you want to be able to call conda from your command line or powershell, you will need to check this box. I believe if you don’t have administrator privileges, you will not be able to install with this box checked. That’s still okay, since conda comes with a program called Anaconda Prompt, which allows you to run conda in a command line window.

After installing, you can open up Anaconda Prompt. If you installed with the “Add Anaconda to my PATH environment variable” checked, you can open up command prompt or powershell to test out if it installed correctly. To test it, type “conda” in the command line. You should see something like this:

Creating an Environment

To create a new virtual environment, we can type:

conda create --name karlsEnv

If you want to specify a specific version of python, you can do this:

conda create --name karlsEnv python=3.4

When you create your new environment, conda will prompt you if you want to download a bunch of base packages. Select yes, since these are base packages that will help you install other packages. To start using the new environment, you type the following:

conda activate karlsEnv

Installing Packages

To install python packages, you can use pip to install packages like so:

pip install requests

You can also install packages from the Anaconda repositories as well. Here’s how to install opencv wtih anaconda:

conda install -c anaconda opencv

When should you install packages with conda or pip? The pip command should really be used for python only packages. Installing packages with Anaconda is good for packages that have dependencies outside of the python environment. Anaconda’s repositories also contain more than just python related packages, but pip is only python packages. A more detailed explanation is here.

Switching Between Environments

Let’s say that you have a bunch of environments and there’s one you want to switch into and activate. You can see a list of all your conda virtual environments by typing in:

conda env list

Now, let’s say are working an environment for a school project named projectenv, and one for a hobby or side project, named hobbyenv. If you are currently in the school project, all you have to do to switch into the side project environment is:

decactivate
conda activate hobbyenv

The deactivate will deactivate the current conda environment you are in. Then the second line lets you activate the hobby project’s environment.

Deleting Environments

Deleting an environment is very easy. Here is what you would type to delete the hobbyenv conda environment:

conda remove --name hobbyenv --all

Summary

Managing your packages is a very important thing when using Python. Packaging can be very confusing even to those who are very experienced, and troubleshooting these issues are not fun. Virtual environments help keep your packages separate from your root installation of python so that the packages you download won’t clutter or break your system. Of all the tools out there that offer virtual environments for python, I favor Anaconda the most for several reasons. Anaconda is very simple to use, can be used with other programming languages, and is easy to use in the corporate world. I hope you have enjoyed this tutorial! You now have the basic knowledge to manage python environments.

Leave a Reply

Your email address will not be published. Required fields are marked *