Manuals / Python / Ch 2

A · SetupBeginner40 min read

2. Virtual environments & toolchain

Python · 52 pages source format

Never pollute global Python. venv per project. pip install inside venv. requirements.txt for reproducibility.

What you'll learn

  • python -m venv
  • activate/deactivate
  • pip install
  • requirements.txt

Create and activate venv

python -m venv .venv creates an isolated environment. Activate before every session.

python -m venv .venv

# macOS / Linux
source .venv/bin/activate

# Windows
.venv\Scripts\activate

python --version
pip --version

Do this now

Create .venv in py-journey. Activate. Run which python (or where python on Windows).

Clear?

Install packages

pip install httpx pytest. pip freeze > requirements.txt commits exact versions.

Do this now

pip install httpx pytest. pip freeze > requirements.txt. Commit both.

Clear?

Editor setup

VS Code / Cursor: select .venv interpreter. Python extension enables run/debug.

Do this now

Open py-journey in editor. Confirm interpreter points to .venv.

Pro tip. Forgot to activate? Symptoms: ModuleNotFoundError for packages you "installed".

Clear?

Checklist