//Python: Virtual Environments

Python: Virtual Environments

Virtual environments are a tool used to separate different python environments, on the same computer. Traditionally, each computer has one installation of the python programming language with its respective set of modules> Those modules as well had one running version. This set-up is not flexible enough for projects that require specific versions of some of those modules or even of python itself. Therefore, we can make use of Virtual Environments to save an environments’ configuration. This way, we can make sure that our projects run on other systems identically as they did on our computers on which we developed them.

Thus, using virtual environments is a great way of making sure that your project runs identically on some other computer than the one you worked on. There are other advantages as well: you can try out your project on multiple python versions and different configurations. For example, if you use certain modules on your project, you wouldn’t want a sneaky versioni update to break parts of your application, would you?

Traditionally, developers would use virtual machines… but that solution is quite costly and time-consuming. Nowadays, a faster and less costly (in computer resources) option for python-based projects is to use a tool like venv.

…Which stands for Virtual ENVironment.

To install venv we will use pip, which is a package manager for python. It is used to install python plugins basically and keep them up-to-date. It’s very easy to use: you just have to give it the name of the plugin (called module) you need.

Short Developer Tutorial

In this short practical introduction to venv, we will go together through the following steps:

  1. install the venv module
  2. create a virtual environment
  3. activate & deactivate the virtual environment
  4. customise it
  5. save its configuration
  6. create a second virtual environment & import configurations

So, to start using venv, open your terminal. (I use cmder on a windows machine).

The requirements is to have python installed and its package manager pip as well. If you’re not sure if you have them installed type into your terminal

python --version

and

pip --version
checking python and pip versions

Next step is to install venv. The pip module is actually named “virtualenv”, so the console command you need to use is “pip install virtualenv“:

installing venv

If you get any errors, make sure that pip is updated. Run this command:

pip install --upgrade pip

…and then try again !

OK, now that we have venv set up on our python environment, let’s go ahead and spin up our first actual python virtual environment.

The command to run is:

virtualenv environment-1

where we named our first environment “environment-1”. You can, of course, name it whatever you want.

creating a virtual environment

if you type “ls” you will see that a new folder has been created.

The next step is to activate the environment that we have just created. to do that, cd into the the “environment-1” folder. Inside there, we have a “Scripts” folder which contains some handy scripts. One of those scripts is “activate” which we will run like in the following screenshot:

activating our first virtual environment

The prompt that mentions the active environment, informs us what environment our machine is currently running.

active environment

to deactivate it, simply run this command:

deactivate

What we have here:

In this folder that we named “environment-1” we run a separate instance of python and pip. Also, in this separate python instance can have whichever python plugins we choose to install onto it. Consider it an isolated test bed (or sandbox).

You can check the python and pip versions and installation directories with these commands (run within “environment-1”):

which python
which pip

and you can check which modules you have installed by running:

pip list
checking the python modules

Let’s install a few modules:

pip install numpy pytz psutil
installing the “numpy” module

now let’s check them in our module list:

As we can see, we are have customised our environment-1 python instance.

Exporting/Importing a configuration

while the environment is active, we can save its configuration in an external file, which we can later import.

To generate the config file type this command:

pip freeze –local > requirements.txt

now, if we ls we can see that our config file has been generated:

We can even check the contents of the file (using the cat command) to verify it:

It contains, indeed, our modules that we previously installed.

Importing configurations

Let’s create a new virtual environment:

virtualenv environment-2

now we have to activate it:

./environment-2/Scripts/activate

and finally, we can import the configuration file:

pip install -r requirements.txt

If we check our installed modules using “pip list” we can see that our second environment is now identical to the previous one.

Conclusion

So today, we learned how to use virtual environments: how to create them, activate & deactivate them, export & import their configurations.

The virtual environments are meant to be separated bundles of your packages (and their versions) that you use in your projects. When activated, the environment affects the system globally.

i.e. You don’t need to build your projects within the virtual environments folders. Your project can be placed anywhere on your computer.

Bonus Cheat Sheet