1.2 Setup and Virtual Environments
A reliable Python setup starts with a local interpreter and a project-scoped virtual environment.
Install and verify Python
Use an official Python 3 installation, then verify from terminal:
python3 --version
Create a virtual environment
Official command:
python3 -m venv .venv
This creates an isolated environment containing:
pyvenv.cfg- interpreter binaries (
bin/on POSIX,Scripts/on Windows) - local
site-packages
Activate and use
source .venv/bin/activate
python --version
You can also run without activation by calling the interpreter directly:
./.venv/bin/python script.py
Install packages
python -m pip install <package>
python -m pip freeze > requirements.txt
Key official guidance
- Virtual environments are disposable and should be recreated when moved.
- They should not be committed to source control.
- Use
sys.prefix != sys.base_prefixto detect active venv runtime.
Official references
venvdocs: https://docs.python.org/3/library/venv.html- Packaging guide (official): https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/