Like A Girl

Pushing the conversation on gender equality.

Code Like A Girl

A guide to Virtual Environments in Python

Python logo on a Black background

Contents

What are virtual environments?

What purpose do virtual environments serve?

How do you create and manage virtual environments the Pythonic way?

The need for virtual environments arose because application dependencies exist. To get a better understanding of virtual environments, it’s necessary to highlight what dependencies actually entail.

A dependency is any piece of software or public package that is plugged into an application to enhance its functionalities.

The type of dependency an application will have largely depends on what it is being built to do, that is, its functionalities.

To conceptualize the essence of dependencies, we could relate it to constructing a house. Basically, you need a piece of land, as a starter. However just a piece of land is not the only thing needed to build a house. Usually you will need to visit a construction marketplace and purchase things such as concrete, wires and cables, doors and frames and the likes.

So in a sense, everything you purchase from that marketplace collectively forms the dependencies of the house.

Similarly, in building an application, you need a programming language/framework to build it upon which is like that piece of land. But most likely that alone will not do. You need to get other software tools and materials to enhance the functionality of your application. These tools and materials are its dependencies.

Languages usually have such marketplace or public repository where you get the extra stuff you need for your application. For example, for the Python language, it is PyPI(Python Package Index). For JavaScript and the node ecosystem, it is Npm. Same goes for other languages.

Managing the Extra stuff

Assuming you have found the dependencies you plan to use in your application, how do you organize and manage all those plenty stuff and keep yourself sane throughout the course of building?

I used the word ‘sane’ because uncool things can happen when your dependencies are not properly managed and organized.

First, you are likely to lose track of what dependencies you used in building an application and if you need to replicate the app or a part of it in future, you will likely end up spending extra time searching for what to use.

Also, keeping up with recent updates of your app’s dependencies is important to prevent vulnerabilities. However, when we do not organize these dependencies, updating them could becomes stressful and it can lead to many risky things like breaking your code, spending extra time fixing bugs plus messing with your sanity. Literally.

Virtual Environments (VEs) keep you organized

Virtual environments create an isolated environment(separate from your system environment) for each application. So this means each application will have a world of its own where you can install all necessary dependencies without worrying about affecting other applications.

Basically creating a virtual environment translates to making a clone of your current system environment which includes your Python interpreter, standard library and other necessary files for each application.

How to Create and Manage Virtual Environments (VE) in Python

Let’s now look at three(3) tools which are the crux of creating and working with virtual environments in Python. Here we go:

►Venv

►Virtualenv and Virtualenvwrapper

Venv : Venv is the standard Python module for creating virtual environments with Python.
To create a virtual environment using venv, open up your terminal, change directory(cd) into the directory you want the virtual environment to live:

1. cd directory-name
2. python3 -m venv virtual-env-name

‘python3’ there specifies the version of Python you want your VE to use. It could be any other version as long as you have it pre-installed on your PC ’cause remember all venv is basically going to do is make a clone of your current Python environment.

To activate the virtual environment(ve) you have just created :

source env-name/bin/activate

And to deactivate it is as simple as typing ‘deactivate’.

P.S: If you are pushing your code to a version control like git, you should not forget to create a .gitignore file and add env to it.

So that’s one way of creating VEs we just discussed above.

Let’s talk about the other two(2).

Virtualenv and Virtualenvwrapper: Virtualenv is a tool built to manage virtual environments and Virtualenvwrapper is (as it suggests) a wrapper for virtualenv. One of the major benefits of using this duo is that you get to use intuitive commands to interact with your VE.

Commands such as: mkvirtualenv, rmvirtualenv, lsvirtualenv (Make, Remove and List virtual environments) and besides that, all your virtual environments can comfortably live in one folder which makes for better organisation.

Virtualenv and Virtualenvwrapper are 3rd party tools (which means they are not part of the Python standard library). So because of this, we have to get it from the Python ‘marketplace’ — PyPi. And to do that, you need to go through some guys which are like the middlemen that help you get stuff there.

These guys are generally called Package Managers. We have a couple of them like: easy_install and Pip but Pip is the most popular and has more benefits.

So, :

pip install virtualenv
pip install virtualenvwrapper

After installation, you need to run a few commands to completely set them up:

export WORKON_HOME=$HOME/.virtualenvs //says I want all my VEs to live in this folder
export PROJECT_HOME=$HOME/Vanessa    //says I want all my projects' home directory to point to this.
source usr/local/bin/virtualenvwrapper.sh   // yo one more thing, this is the location of the script for this package just in case we need it.

Yep! And we are all set to start creating virtual environments for our projects using virtualenv.

So we could try out some commands:

mkvirtualenv stuff

When you first type the command above, you may see something like: ‘command not found’.

All you need to do is run the package script:

source usr/local/bin/virtualenvwrapper.sh

The script contains instructions that sets some temporary variables on your system and configures it to use virtualenv. Now re-typing the first command or using the upper arrow-key to bring back the command should create a shiny new virtual environment for you.

Your virtual environment is now created and automatically activated. You can begin to install dependencies and stuff that would be useful for building your application without messing up other projects.

Some other commands to experiment with:

1. deactivate (exits the VE) 
2. workon [ve-name] :  (to switch to a virtual environment) simple equivalent to typing 'source env-name/bin/activate' when using Venv.
3. showvirtualenv [env-name] : displays details of the VE.
4. rmvirtualenv [env-name] : do away with a VE

Check out the virtualenv command reference for more.

Also check out these tips for working with Virtualenvwrapper and virtualenv with pip.

And it’s wrap! Please share this if you found it helpful. I’m happy to connect on Twitter as well.

Thanks to Iso Sam