0% complete

Python foundations · Lesson 2 of 3

Install Python and manage project environments

Choose a Python installation and isolate each project’s dependencies.

Lesson

1. Concept and mental model

Use a currently supported Python release from python.org or your organization’s approved package source. Confirm the active interpreter with python --version or, on some systems, py --version. Multiple Python versions can exist on one machine, so verify which executable a terminal or editor actually runs. A virtual environment creates a project-local installation context; it prevents one project’s library upgrades from silently breaking another project.

2. How it works

Create an environment with python -m venv .venv, activate it for the current shell, then install packages using that environment’s python -m pip. Using python -m pip reduces ambiguity when several pip commands point to different interpreters. Record direct dependencies in a requirements file or project metadata and install from that record on another machine. Do not commit the environment folder itself; it contains machine-specific files and can be recreated.

3. Use it safely and effectively

Treat dependency upgrades as changes that need review and tests. Pinning every transitive dependency may be appropriate for reproducible deployments, while a library project may declare a compatible version range. Do not install packages with administrator privileges merely to resolve a project-local issue. If a command behaves unexpectedly, inspect python -m pip --version and the interpreter path before changing the global machine configuration.

4. Syntax reference

python -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install requests

5. Guided walkthrough

Create the environment from the project directory, activate it in the current terminal, and confirm that python and pip point inside .venv. Install only the libraries this project needs and record them so another developer can recreate the setup.

6. Common mistakes to avoid

  • Installing a dependency globally because the wrong interpreter or pip executable was selected.
  • Committing the generated .venv directory instead of the dependency manifest.

Visual guide

A repeatable Python setup

  1. Step 01 Select

    Choose and verify the Python interpreter.

  2. Step 02 Isolate

    Create and activate a virtual environment.

  3. Step 03 Install

    Install and record project dependencies.

Code examples

Worked example
POWERSHELL
python -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install requests
Expected output
A project environment is activated and requests is installed into it.
Verify the environment on Windows
POWERSHELL
python --version
python -m pip --version
where.exe python
Expected output
The interpreter and pip paths identify the active environment.
Record and reinstall dependencies
POWERSHELL
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt
Expected output
Writes installed package versions and installs them from the manifest.

Key points

  • Choose a Python installation and isolate each project’s dependencies.
  • Prefer explicit, testable behavior over hidden side effects.
  • Validate assumptions at system boundaries and handle failure deliberately.

Check your understanding

Practice

Why use a virtual environment?

Your learning progress

Finish this lesson