Breakdown
python3 -m venv venv
python3
:
This is how you invoke the Python version 3.0+.
-m
:
This is telling python that you want to run a module as a script.
So in this instance you are telling Python to run module venv
.
venv
:
This is the Python virtual environment module you are instructing to be run.
venv
:
This is going to be the name of the virtual environment that is going to be created.
You can name it whatever you want realistically, but venv is the common convention.
Let me explain
When you run python3 -m venv venv
The following is created in your project where you ran that command:
├── venv/
│ ├── bin/ # On Unix/macOS (Scripts/ on Windows)
│ ├── include/
│ ├── lib/ # Lib/ on Windows
│ └── pyvenv.cfg
A new directory called venv is created Within venv/ subdirectories are also created.
Subdirectories
venv/bin/ (venv/Scripts on Windows) The bin directory is going to contain certain necessary executables a long with the Python interpreter.
venv/include/ This subdirectory contains files necessary for compiling certain Python packages.
venv/pyvenv.cfg This is the metadata file. For instance this would contain info such as Python version being used.
venv/lib/ (venv/Lib on Windows) This has the site-packages directory where the installed Python packages live.
Once the virtual environment is established, one needs to run the activate
script.
To activate the virtual environment run: source venv/bin/activate
TL;DR
The main take away here is that the activate script changes your shell session to use the Python virtual environment.
The activate
script modifies the `PATH`
vairable so that specific items related to the virtual envorinment are used, such as the Python interpreter and executables. This isolates the dependencies and settings from your project and the global envrinment.