# Python Virtual Environment ## Overview | Action | Command | | - | - | | <a href="#install-required-packages">Install</a> | `sudo apt install python3-venv` | | <a href="#create">Create</a> | `python3 -m venv .venv` | | <a href="#activate">Activate</a> | `source .venv/bin/activate` | | <a href="#deactivate">Deactivate</a> | `deativate` | ## Install Required Packages ```bash sudo apt install python3-venv ``` - installiert das python modul venv zum erstellen von virtual environments ## Create ```bash 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 ```bash 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: ```bash (venv) <user>@raspberrypi:~/<project> $ ``` ### Alternative - alternatively the python virtual environment can directly be called (e.g. from scripts): ```bash /home/<user>/<project>/.venv/bin/python ... ``` ## Deactivate ```bash deactivate ``` - deactivates the active virtual environment
