Appendix B — Python Development Environment

A Python environment is the place where Python runs and where installed packages live. Keeping one project isolated from another saves you from a lot of mysterious breakage later.

Keep in mind Python is used for a great many different reasons, so how you specifically manage environments is up to you. For this course we only need the boring, useful version: one project, one environment, packages installed in the place your project actually uses.

Create a virtual environment

Python includes venv, which is enough for this course.

$ python3 -m venv .venv

On Windows:

> py -m venv .venv

.venv is just a directory name. It’s common, easy to recognise, and already ignored by this repository.

You can call the directory almost anything: the name of your project, the street you grew up on, or something wonderfully unhelpful that you’ll regret in three weeks. .venv is the convention because it’s short, obvious, and less likely to bring future confusion.

Activate it

On macOS and Linux:

$ source .venv/bin/activate

On Windows PowerShell:

> .venv\Scripts\Activate.ps1

Once activated, your prompt may change to show the environment name. From this point, packages installed with python -m pip should go into the virtual environment.

That little prompt change is your reminder that you’ve stepped inside the project bubble, and from here on, anything you install belongs to this project rather than being flung into a random global Python.

Install packages

For a single package:

$ python -m pip install requests

For this book repository, use the dependency file provided by the project:

$ python -m pip install -r requirements.txt

That command asks pip to install each package named in requirements.txt into the active environment. uv and conda are still good options if they already fit the way you work, but the project itself only needs the requirements file.

The important bit is consistency: if you can delete the environment and rebuild it from a file, you’ve made your project easier for other people to run and easier for you to resurrect later.

Editors

Use the editor you’re comfortable with. VS Code, PyCharm, JupyterLab, Vim, and other editors all work fine. The important thing is that your editor is using the same environment you activate in the terminal.

When in doubt, run this in your editor and in your terminal and compare the paths:

import sys
print(sys.executable)

If the paths differ, your editor and terminal are using different Python interpreters. That mismatch is a classic source of “works in the terminal, explodes in the editor” confusion.

Checkpoint

Create and activate a virtual environment, then run python -m pip --version. Confirm that the path shown by pip points inside the virtual environment. If it does, excellent: your little project bubble is doing its job.

Extension Activity

Install requests in the virtual environment and run python -c "import requests; print(requests.__version__)". Then deactivate the environment and explain why the same import may or may not work outside it.

References