,

Installing virtualenv and virtualenvwrapper with Homebrew on macOS

For this tutorial I assume that you followef install Python with pyenv to learn how to install multiple versions of Python and how to switch between them. This is great for starting to write code in Python but very soon you want to install additional packages and module to extend the functionality of Python.

Now imagine this:

You are working with a particular version of Python and you installed additional packages for working on data with numpy or pandas. Then you want to work on a different project using django which you also install as an additional package. All these installed packages are bound to the python version you use. The number of packages will grow over time and they will always need resource whether you need them in your project or not. They will be there. You might say you don’t care because your computer is strong enough. Ok, but what about using different versions of your packages for different projects? You really don’t want to upgrade or downgrade your packages with all their dependencies every time you switch between projects. How cool would it be you could easily switch like with the Python version? Well, that’s what virtualenv and virtualenvwrapper are for. virtualenvwrapper is a set of extensions for virtualenv to make live easier. You can create virtual environments with a fixed version of Python that is bound to a particular set of packages. So you can have dedicated environments for each project and you can easily switch between them. This becomes handy when you want to check if your project runs with a never version of some packages. You can create a new virtual environment with all the packages versions you want to try and let your project run in it without touching your original environment.

 

Installing virtualenvwrapper

For the sake of simplicity, I will guide you through the installation of virtualenv and virtualenvwrapper together. If you look around you will find a lot of tutorials for virtualenv alone. Don’t wonder, the syntax for creating, activating and switching virtual environments is different then.

To install virtualenv and virtualenvwrapper globally run

				
					$ pip install virtualenv
$ pip install pyenv-virtualenvwrapper
				
			

Append the following lines to the .bash_profile or .zshrc file in your home folder. This depends on which shell you use. If you are not sure, just go back to Installing Python with Pyenv on macOS to see how you can find out.

				
					export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"
export WORKON_HOME=$HOME/.virtualenvs
pyenv virtualenvwrapper_lazy
				
			

The last thing to do is to reload your shell. Again, depending on which one you are using, run one the following commands:

				
					$ source ~/.bash_profile
				
			

or

				
					$ source ~/.zshrc
				
			

Now you can start using virtual environments. Please be aware that every time you install an use a new version of python, you need to install virtualenvwrapper or virtualenvwrapper_lazy (preferred) by running the command

				
					$ pyenv virtualenvwrapper_lazy
				
			

You might have noticed that you added this line to your .bash_profile or .zshrc file. This runs the command every time you start your terminal. In other words, if you reload your shell after installing a new Python version, virtualenvwrapper_lazy will be installed for you.

Basic virtualenvwrapper shell commands

Creating a new virtual envrionment

				
					$ mkvirtualenv my_python_env
				
			

This will create the virtual environment with the name my_python_env and your shell will automatically activate it. You should see an output similar to

				
					(my_python_env) $ 
				
			

If you are in a virtual environment, you can leave it with 

				
					(my_python_env) $ deactivate
				
			

This will take you back to your regular shell and the name of the virtual environment (in the brackets) will disappear.

				
					$
				
			

If you want to manually active a virtual environment, you can use the workon command in combination with the name of the environment. For example:

				
					$ workon my_python_env
				
			

Of course you can also remove virtual environments that you don’t need anymore. This is as easy as this:

				
					$ rmvirtualenv my_python_env
				
			

Resources

If you want to learn more about virtualenv and virtualenvwrapper, I recommend to visit the following websites:

Related articles

Nobrainer Programming

Hey, how is it going? I’m Sven and Nobrainer Programming is me trying to share some knowledge, experiences and thoughts on learning how to code. Before a became a professor for Sports- and Health-Informatics, I was working for almost 10 years for one of the worlds largest research centers for artificial intelligence. Over the years I taught numerous students how to code. It’s quite easy and everyone can learn it. So, let’s go!

Explore