Blame
|
1 | # Python Virtual Environment |
||||||
| 2 | ||||||||
| 3 | ## Overview |
|||||||
| 4 | | Action | Command | |
|||||||
| 5 | | - | - | |
|||||||
| 6 | | <a href="#install-required-packages">Install</a> | `sudo apt install python3-venv` | |
|||||||
| 7 | | <a href="#create">Create</a> | `python3 -m venv .venv` | |
|||||||
| 8 | | <a href="#activate">Activate</a> | `source .venv/bin/activate` | |
|||||||
| 9 | | <a href="#deactivate">Deactivate</a> | `deativate` | |
|||||||
| 10 | ||||||||
| 11 | ## Install Required Packages |
|||||||
| 12 | ```bash |
|||||||
| 13 | sudo apt install python3-venv |
|||||||
| 14 | ``` |
|||||||
| 15 | - installiert das python modul venv zum erstellen von virtual environments |
|||||||
| 16 | ||||||||
| 17 | ## Create |
|||||||
| 18 | ```bash |
|||||||
| 19 | python3 -m venv .venv |
|||||||
| 20 | ``` |
|||||||
| 21 | - executes python3 of PATH (? default python3 of the system) |
|||||||
| 22 | - executes modul venv (for virtual environment) |
|||||||
| 23 | - creates new directory .venv |
|||||||
| 24 | - initializes virtual python environment in .venv/ |
|||||||
| 25 | - creates the following structure: |
|||||||
| 26 | ``` |
|||||||
| 27 | /home/<user>/<project>/ |
|||||||
| 28 | |---.venv/ |
|||||||
| 29 | | |--- bin/ |
|||||||
| 30 | | |--- lib/ |
|||||||
| 31 | | |--- ... |
|||||||
| 32 | |--- <myprogram.py> |
|||||||
| 33 | ``` |
|||||||
| 34 | ||||||||
| 35 | ## Activate |
|||||||
| 36 | ```bash |
|||||||
| 37 | source .venv/bin/activate |
|||||||
| 38 | ``` |
|||||||
| 39 | - activates the python virtual environment within .venv/ |
|||||||
| 40 | - all following python executions are made from within this virtual environment |
|||||||
| 41 | - console inserts virtual environment name at beginning: |
|||||||
| 42 | ```bash |
|||||||
| 43 | (venv) <user>@raspberrypi:~/<project> $ |
|||||||
| 44 | ``` |
|||||||
| 45 | ### Alternative |
|||||||
| 46 | - alternatively the python virtual environment can directly be called (e.g. from scripts): |
|||||||
| 47 | ```bash |
|||||||
| 48 | /home/<user>/<project>/.venv/bin/python ... |
|||||||
| 49 | ``` |
|||||||
| 50 | ||||||||
| 51 | ||||||||
| 52 | ## Deactivate |
|||||||
| 53 | ```bash |
|||||||
| 54 | deactivate |
|||||||
| 55 | ``` |
|||||||
| 56 | - deactivates the active virtual environment |
|||||||
