with Anaconda
For months now I have been working with data out of Jupyter notebooks in an Anaconda (conda) virtual environment pip installing Python package after Python package, naively unaware of how anything could possibly go wrong with my computer’s ever growing library of PyPI – all until a few conflicts arose and the code I was working with indicated it was dependent on an earlier versions of a packages I had already installed.
If you are beginning to work with data in Python and furiously learning the endlessly growing library of packages that come along with knowledge of Python, here is my word of advice to you: you will save yourself hours of bug fixing and backtracking by knowing how to work with Python virtual environments from the start. If you haven’t already, start by downloading Anaconda. This can take a little while, so go brew yourself a little coffee (or tea!) while it downloads and then you can finish the rest of this blog in about 5 minutes.

Virtual environments are simply put – a way for you to isolate your set of dependencies for every project.
By setting up individual virtual environments you will save yourself (and others who may want to add to or reproduce your code) from endless frustration. If you have ever experienced breakage in your code because of differing package version requirements or obsolescence (← say that word out loud) of any kind, Python virtual environments are your solution. With virtual environments you’ll be able to set up and navigate specialized working environments for all of your unique projects right from the command line (or the Anaconda prompt window if using Windows).
Now that you have a conceptual understanding of what a virtual environment is and have downloaded conda, we’ll cover some basics for how to set up, navigate, update, and delete your virtual environments, and you’ll be on your way to flawless production in no time at all.
I learned all of this from the conda website itself, so if you want to take this for a deeper dive, this is my recommended starting point.
Step 1: Creating and activating a new virtual environment
With conda now installed, as soon as we open it we will already be in the conda (base) virtual environment. We don’t want to add any packages to our base environment as this will be our clean slate, our home "base." Instead, we will create a new environment for whatever project we want to start working on. Open up a terminal window and type the command below. You can replace my_env with any name you would like for your virtual environment, and choose the version of Python you want in your environment (by default it will be the version of Python you had when you installed conda):
conda create --name my_env python=3.8.3
Your new environment is created once the terminal prompts you asking whether you want to proceed ([y]/n)? Type y and hit enter. Now it will ask if you want to activate the environment. To activate it follow the command line prompt and type:
conda activate my_env
You will know your environment has been activated because the name you gave will be displayed in parentheses on the left side of the terminal.

Step 2: Updating your virtual environment
In our new virtual environment we can add packages with pip install like any other project, the advantage of the environment being that any new installations won’t affect other parts of your computer, only the virtual environment we have set up. I am working on a natural language processing project right now, so I am going to install the Natural Language Toolkit (nltk) to my environment like this:
pip install nltk
Now this package is installed in the environment I have setup for my NLP project, and won’t cause any conflicts for me elsewhere or down the line.
Step 3: Navigating multiple virtual environments
To navigate between environments is easy. To deactivate an environment you can simply type the command conda deactivate
. This will take you back to the base environment. A quicker way to switch directories is to just activate the new directory you want to switch to, for example:

As you can see above, I started out in the my_env environment and then made a quick switch to a different environment titled nlp_env by activating it with the conda activate
command. In the next line I used this command
conda info --envs
which pulls up a list of all of my environments and the asterisk indicates which environment is active.
Step 4: Deleting virtual environments
Deleting or removing an environment can also be done with one line of code. Make sure you have deactivated the environment you want to delete and then run this line of code
conda remove --name my_env --all
Conclusion
I can’t emphasize enough how helpful this will be for you moving forward. The Anaconda app has plenty more functionality and a helpful GUI, but if you are using Python less for Data Science related projects and don’t find yourself using Anaconda as a resource, I recommend installing pipenv from PyPI to set up the same framework of separate virtual environments for any of your programming projects. The actual Python documentation is a little outdated when it comes to using virtual environments, so I recommend sticking to conda and pipenv. Hope this helps, and may it spare you many a headache!