Environments and dependencies
Isolate packages per project and make installs reproducible.
- Create and activate a virtual environment
- Install and freeze dependencies
- Avoid committing local environments
A virtual environment keeps a project's packages separate. Requirements pin exact versions so another machine installs the same set.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install requests==2.32.3
pip freeze > requirements.txt- 01Create
Run python3 -m venv .venv in the project root.
- 02Activate
Use source .venv/bin/activate on macOS or Linux, .venv\Scripts\activate on Windows.
- 03Install
Install into the active environment with python -m pip install.
- 04Pin
Save exact versions with pip freeze > requirements.txt and commit that file.
Write the setup commands
Create a virtual environment, install pytest 8.3.3 and save the exact versions.
# write the setup commandsWhy pin versions in requirements.txt?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.