# The reason of using venv

The reason why I use venv and not another virtual environment in python is explained in this stackoverflow answer:

venv creates virtual environments in the shell that are fresh and sandboxed , with user-installable libraries, and it’s multi-python safe. Fresh because virtual environments only start with the standard libraries that ship with python, you have to install any other libraries all over again with pip install while the virtual environment is active. Sandboxed because none of these new library installs are visible outside the virtual environment, so you can delete the whole environment and start again without worrying about impacting your base python install. User- installable libraries because the virtual environment’s target folder is created without sudo in some directory you already own, so you won’t need sudo permissions to install libraries into it. Finally it is multi-python safe, since when virtual environments activate, the shell only sees the python version (3.4, 3.5 etc.) that was used to build that virtual environment. – Stackoverflow

# Install python3-venv

You can read the venv docs here

This virtual environment is shipped with Python 3 with the standard library, so it can be called as a module of it. In some system is not include in the base installation so is necessary to install as a third part, for example in Debian is necessary to install this tool with the package manager:

apt install python3-venv


This tool is usefull without a package manager for python that allowed to install, remove and update packages. This task is well perform by Pip, this is the most used tools in python to install packages from the Python Package Index.

You can read the pip docs here.

Pip is a third part and is not in the standard library, in some distribution is necessary to install it separately. Like Debian:

apt install python3-pip


# Create and activate environment

To create a new virtual environment with venv run this command:

python3 -m venv env


This will generate a directory called env that contains the virtual environment root with all the resources necessary, like the binaries and libraries.

To use this environment is necessary to change the python and binary path. There is a script called activate that can be found inside the bin folder, that provide this switch of environment:

source env/bin/activate


The source command is a bash shell built-in that read an execute commands from file in the current shell environment and return the exit status of the last command executed.

The activate file is a bash script that:

1. Define the deactivate command, use it to exit from the virtual environment
2. Store in variable the current binary path and PYTHONHOME path
3. Replace the binary path and the PYTHONHOME path if exists

To deactivate the environment is possible to run the deactivate command created by the activate source file:

deactivate


This command reset the python and binary home of the user and remove all the references in the shell of the virtual environment.

# Use the venv environment

Once the source command is run all the pyhton libraries and modules are refer to this directory. We can now install and remove python modules, using pip, without affect the user environment.

For example in a project we have to use the numpy dependency, to satisfy this request is necessary to create first the virtual environment:

python3 -m venv env


Than activate it:

source env/bin/activate


To get the third libraries use in this environment use the freeze argument of pip:

pip freeze


The output in a fresh environment should be this:

pkg-resources==0.0.0


Now let’s install numpy:

pip install numpy


If everythings goes ok the new freeze output should be this:

numpy==1.17.0
pkg-resources==0.0.0


Is now possible to use numpy in this environment.

# Reproducibility of environment

Reproduce the environment is necessary when the project is not only on local machine or if we collaborate with other developer. The simple way to share the requirements of the environment is to create a file with all the dependency, by convention this file is called requirements.txt. To create this file we use the freeze argument of pip:

pip freeze > requirements.txt


This command save the output of freeze into a file called requirements.txt.

With this file we can regenerate or create the same environment by run:

pip install -r requirements.txt


All the packages found in the file will be install or update.

##### Gabriele Puliti
###### Software Developer and Student

Learn, Develop, Sleep, Repeat

Previous