python venv

Introduction

When using that ?
In almost every applications.
The python and packages installed on the host may suit to some applications needs but not to all.
To settle that : the venv module. That is the standard python module that allows applications to use a specific version of python and specific additional packages.

Where are stored the resources of virtual environments ?
For each virtual environment, a directory with the chosen environment name will be created and hosted all resources for the env.

Creating and using a Virtual Environments

Create the env :
1) go into the directory that should host that, generally the root directory where the application code is stored.
2) execute : python3 -m venv foo-env
As a result the foo-env directory that contains the env resources is created.
Note : the env directory is not designed to contain the source code of the application. That is kept in its parent directory.

Activate the env : (it means : bind the current shell to the python conf/install of that venv)
execute : source foo-env/bin/activate
Now, the shell should be prefixed by (foo-env)

In windows with git bash, we need to use this syntax:
. venv/Scripts/activate

Deactivate the current env : (it means : unbind the current shell to the python conf/install of that venv)
execute : deactivate
Now, the shell should not be prefixed by (foo-env)

Executing python of a specific env without activate it :
In some cases (scripts, crontab, …), we don’t need to execute python in the frame of an interactive terminal.
We just want to execute one command and that’s all.
To achieve that with venv : we could use the python binaries located in the foo-app-env/bin folder.
For example : /home/foo/foo-app/foo-app-env/bin/python foo.py

Package managements with Virtual Environments

Requirement : enable the wished venv.

list packages installed in the virtual environment:
pip list

install in the virtual environment one or multiple packages:
pip install foo-package bar-package ...

Make the virtual environment to be usable by other hosts

Motivations :
– A python application may be to deployed on multiple hosts.
– A python application may be versioned and coded by multiple developers.

freeze command describes the deps and their versions in a format than the install command understand to setup the deps of an application in a virtual environment.

2 steps :

1) Generate an output of the packages installed in the current virtual environment in a  « requirement » format
pip freeze
Generally we save the output into a file such as :
pip freeze > requirements.txt
We version/distribute that file.

2) install in the current virtual environment the packages specified in the « requirements » file 
pip install -r requirements.txt

Common issues

Problem :
pip install -r requirements.txt fails because of a package that doesn’t exit : pkg-resources==0.0.0
Cause :
On debian-based distros, pip freeze in venv may generate a wrong package :
pkg-resources==0.0.0
Fix :
Remove that line in the output generated by freeze :
pip freeze | sed '/pkg-resources==0.0.0/d' > requirements.txt

Ce contenu a été publié dans Non classé. Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *