Python Virtual Environment

Overview

Action Command
Install sudo apt install python3-venv
Create python3 -m venv .venv
Activate source .venv/bin/activate
Deactivate deativate

Install Required Packages

sudo apt install python3-venv
  • installiert das python modul venv zum erstellen von virtual environments

Create

python3 -m venv .venv
  • executes python3 of PATH (? default python3 of the system)
  • executes modul venv (for virtual environment)
  • creates new directory .venv
  • initializes virtual python environment in .venv/
  • creates the following structure:
    /home/<user>/<project>/
    |---.venv/
    |    |--- bin/
    |    |--- lib/
    |    |--- ...
    |--- <myprogram.py>

Activate

source .venv/bin/activate
  • activates the python virtual environment within .venv/
    • all following python executions are made from within this virtual environment
  • console inserts virtual environment name at beginning:
(venv) <user>@raspberrypi:~/<project> $

Alternative

  • alternatively the python virtual environment can directly be called (e.g. from scripts):
/home/<user>/<project>/.venv/bin/python ...

Deactivate

deactivate
  • deactivates the active virtual environment
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9