6.1. Module venv

6.1.1. Rationale

  • Bundled with Python since 3.3

  • Isolated Python environment

  • Allows to have multiple versions of Python for one project

  • For testing on different versions: py37, py38, py39, py310a1

  • Test libraries and frameworks before upgrading (create venv, install requirements, run tests, delete if fails)

  • Allows to have different versions of libraries and frameworks for each project

6.1.2. Create

  • DIRECTORY is the name of venv folder (see "Directory Naming Convention" below)

$ cd PROJECT
$ python3.9 -m venv DIRECTORY
Code 6.65. Example
$ cd PROJECT
$ python3.9 -m venv venv-3.9.0

6.1.3. Activate

  • Scripts for Windows

  • bin for macOS, Linux, BSD

  • Note the direction of slash and backslash (OS dependent)

Code 6.66. Windows
$ cd PROJECT
$ venv-3.9.0\Scripts\activate.bat
Code 6.67. macOS, Linux, BSD
$ cd PROJECT
$ venv-3.9.0/bin/activate

6.1.4. Directory Naming Convention

  • No standard naming convention

  • Naming directory like module (venv) name is a good idea

  • Adding Python version is also a good practice

  • Optionally naming per main framework/library version

  • Dot at the beginning hides directory on Linux and macOS (but doesn't work on Windows)

  • Underscore is Python convention for private/protected, but does not work for OS and Git

venv/
.venv/
_venv/

virtualenv/
.virtualenv/
_virtualenv/

py
python
py-3.9
python-3.9
python-3.9.0

.py
.python
.py-3.9
.python-3.9
.python-3.9.0

venv-3.6/
venv-3.7/
venv-3.8/
venv-3.9/
venv-3.10/

venv-3.8.0/
venv-3.8.1/
venv-3.8.2/
venv-3.8.3/

venv-3.9.0/
venv-3.9.1/
venv-3.9.2/
venv-3.9.3/

venv-3.10-alpha1/
venv-3.10-alpha2/
venv-3.10-beta1/
venv-3.10-beta2/
venv-3.10-rc1/

venv-django-2.1
venv-django-2.2
venv-django-2.3
venv-django-3.0
venv-django-3.1
venv-django-3.2
venv-django-4.0a1
venv-django-4.0a2
venv-django-4.0b1
venv-django-4.0b2
venv-django-4.0rc1
venv-django-4.0rc2

venv-py39-dj33
venv-python39-django33
venv-python310alpha1-django40a1
Code 6.68. This convention is from virtualenv-wrapper module (mostly used in Python 2)
~/.virtualenv/PROJECT_NAME/

6.1.5. Good practices

  • name as version venv-3.9.0

  • place in your project directory and add folder to .gitignore (important!)

  • otherwise place it in ~/.virtualenv/, but some meaningful name is required

  • Append at the end of venv-3.9.0/bin/activate:

    project_name='My Project'
    
    red='\[\033[00;31m\]'
    green='\[\033[00;32m\]'
    blue='\[\033[00;36m\]'
    white='\[\033[00;39m\]'
    
    export PS1="\n${blue}${project_name}> ${white}"
    

6.1.7. Assignments

6.1.7.1. Virtualenv

  • Assignment: Virtualenv

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 0 lines

  • Estimated time of completion: 2 min

  • Filename: solution/venv.py

English:
  1. Create venv

  2. Add venv as a Python interpreter in your IDE

Polish:
  1. Stwórz venv

  2. Dodaj venv jako interpreter Python w Twoim IDE