4. venv

4.1. venv Module

  • What is venv

  • Isolated Python installation for project

  • Allows to have multiple versions of Python for one project (i.e. for testing)

  • Allows to test libraries and frameworks before upgrading

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

4.2. Create venv

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

cd PROJECT
python3.7 -m venv DIRECTORY

4.2.1. 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)

Listing 138. Explicitly states which Python version is installed
.venv/
venv/
virtualenv/

venv-3.6/
venv-3.7/
venv-3.8/

venv-3.7.0/
venv-3.7.1/
venv-3.7.2/
venv-3.7.3/
venv-3.7.4/

venv-3.8-beta1/
venv-3.8-beta2/

venv-django-2.1
venv-django-2.2
venv-django-3.0beta1
venv-django-3.0beta2

venv-py37-dj22
venv-python37-django22
venv-python38beta-django30beta
Listing 139. This convention is from virtualenv-wrapper module (mostly used in Python 2)
~/.virtualenv/PROJECT_NAME/

4.2.2. Example

cd PROJECT
python3.7 -m venv venv-3.7.4

4.3. Activate Virtualenv

4.3.1. Windows

cd PROJECT
venv-3.7.4\Scripts\activate.bat

4.3.2. macOS, Linux, BSD

cd PROJECT
venv-3.7.4/bin/activate

4.4. Good practices

  • name as version venv-3.7.4

  • place in your project directory

  • add folder to .gitignore (important!)

  • Add to venv-3.7.4/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}"
    

4.5. Assignments

4.5.1. Virtualenv

  • Lines of code to write: 0 lines

  • Estimated time of completion: 2 min

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