Python Foundations·Quality and delivery

Environments and dependencies

Isolate packages per project and make installs reproducible.

Beginner 10 min read Lesson 15 of 480 Challenge included
What you will be able to do
  • 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.

setup.shTerminal
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install requests==2.32.3
pip freeze > requirements.txt
  1. 01
    Create

    Run python3 -m venv .venv in the project root.

  2. 02
    Activate

    Use source .venv/bin/activate on macOS or Linux, .venv\Scripts\activate on Windows.

  3. 03
    Install

    Install into the active environment with python -m pip install.

  4. 04
    Pin

    Save exact versions with pip freeze > requirements.txt and commit that file.

Your turn

Write the setup commands

Easy

Create a virtual environment, install pytest 8.3.3 and save the exact versions.

BASH · starter
# write the setup commands
Check your understanding before moving on.
Knowledge checkQuestion 1

Why pin versions in requirements.txt?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Project: course tracker
Lesson 15 of 480 · 0% of course