Working with Virtual Environments


How to use the venv module

Introduction

Virtual environments are a honking great idea, it lets you create isolated python environments, where you can install packages that won't affect with any package in the global python installation in your system.

logo

But, why? what problem does virtual environments solve? Well it solves the problem of handling compatibility issues between different versions of packages. Let's suppose you are using a package called FortuneCookie for your project, the latests version of this package in the PyPI is 0.2 and according to the documentation, this package can be used as follows.

import fortune_cookie
print(fortune_cookie.fortune())

And this will print a fortune cookie phrase for you, cool right? Now we can add this package in the requirements.txt file specifying the version 0.2, and the code that uses this package is already delivered.

Eventually the author of FortuneCookie, for some reason, updates the package and renames the method from fortune to wishmeluck. You can update to the latest version of this package and use this new method name for your new projects. But at some point you have to make some fixes in your previous project, and since you have the latest version of FortuneCookie installed your python code will crash.

How to solve this? you have two options, you can downgrade the version of the package to a previous version, or you can fix the code by renaming all the fortune method calls to wishmeluck. The first option makes you upgrade and downgrade every time you move from one project to the other. The second option even that sounds simple, it is an exhausting job for really big projects (this is just a small exercise).

So in order to avoid these pitfalls, we can use a virtual environment for each project.

How to create a Virtual Environment

Now that we understand the need for virtual environments, let's see how to create one.

If you have installed python 3 in your system, then you already have the venv module installed. To create a new virtual environment we need to choose a directory for this, usually the most common place is the root directory for the project, along with the requirements.txt file, using our terminal in the chosen directory we can execute the following command.

python3 -m venv name

For the name argument we can choose any name for the virtual environment, some recommendations is to choose a short and meaningful name respect to the project, for example, cloud-env.

python3 -m venv cloud-env

This will create a new directory inside the chosen directory with the provided name, cloud-env in our case. Inside this directory there is a small copy of the python system files. Inside you will find 3 folders include, lib and bin.

For Windows users the folders found are Include, Lib and Scripts.

In both cases new installed packages will be installed in the site-packages directory inside the lib or Lib folder.

How to activate a Virtual Environment

In order to activate a virtual environment we need to execute the bash or executable file called activate located inside the bin directory for Linux or mac users, or Scripts directory for Windows users.

source env/bin/activate

or

env\Scripts\activate

you will verify that your virtual environment is activated by seeing the following prefix in your terminal.

(cloud-env)

cloud-env in our case, keep in mind that you can choose any name for this.

Installing packages

Now that we have the virtual environment already setup, we can install packages.

pip install FortuneCookie==0.2

Or we can install from a requirements.txt file.

pip install -r requirements.txt

How to deactivate a Virtual Environment

The fastest way to deactivate a virtual environment is to quit the current terminal session, but back in the days this could cause issues with some lock files and damaging your virtual environment, to avoid this we can deactivate it in a safer way by executing the deactivate bash or executable.

For Linux users we can just use the following.

deactivate

For Windows users.

env\Scripts\deactivate

Thanks for reading.

0 Comments

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel