← All lessons
Python advanced · Lesson 11 of 11

Real projects and speed

Writing code is half of it; the other half is setting a project up so it runs the same on every computer, can be tested, and stays fast. These are the habits of working Python developers.

Virtual environments and packages

Different projects need different versions of the same packages. A virtual environment is a folder holding a private copy of Python's package space for one project, so installing a package for one never breaks another. Make one in the project folder, activate it, then install into it:

python -m venv .venv
source .venv/bin/activate        # on Windows: .venv\Scripts\activate
pip install requests pytest
pip freeze > requirements.txt

A project's settings and dependencies live in pyproject.toml, the standard file that tools like pip, pytest, ruff and uv all read. Newer tools such as uv and Poetry create the environment, install packages and keep a lock file of exact versions for you, so everyone gets the same set.

Layout, logging and style

A common layout keeps the code in a package (a folder with an __init__.py) under src/, tests in tests/, and the project file at the top. A linter like ruff spots mistakes and style problems, and a formatter (ruff format, black) lays code out the same way every time, so reviews are about what the code does.

For messages from a running program, use the logging module rather than print(). Each message has a level (DEBUG, INFO, WARNING, ERROR, CRITICAL), so you can show everything while developing and only warnings in production, with times and sources added automatically.

import logging, sys

logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s", stream=sys.stdout, force=True)
log = logging.getLogger("shop")
log.debug("not shown at INFO level")
log.info("order %s placed", 1042)
log.warning("stock low: %d left", 3)
Prints
INFO shop: order 1042 placed
WARNING shop: stock low: 3 left

Making code fast

First rule: measure before changing anything; programmers are famously bad at guessing where the time goes. timeit times a small snippet, and cProfile (run as python -m cProfile -s cumtime app.py) lists which functions take the time. Then fix the biggest one, which is usually the algorithm, not the details:

  • x in a_list checks every item; x in a_set or a dict lookup is near instant. Turning a list you search often into a set is the most common big win.
  • A loop inside a loop over the same data is n² work: 1,000 items means a million steps. A dict built once usually turns it into one pass.
  • Joining strings with += in a loop copies them again and again; collect pieces in a list and "".join() them once.
  • Built-ins (sum, sorted, min, comprehensions) run in fast C code. For number crunching, NumPy and pandas do whole arrays at once.
  • Don't repeat work: @cache from the functions lesson remembers results.
import timeit

items = list(range(10_000))
lookup = set(items)
slow = timeit.timeit(lambda: 9_999 in items, number=200)
fast = timeit.timeit(lambda: 9_999 in lookup, number=200)
print(fast < slow)
Prints
True

That's the whole path. From here, build something you'll use: a script that tidies your downloads folder, a small web app, a data analysis of something you care about. Real projects teach the rest.

Practice

Why give each project its own virtual environment?

More info

Quick reference

python -m venv .venvMake a virtual environment; activate it, then pip install.
pyproject.tomlThe project's settings and dependencies, read by most tools.
ruff / black / pytestLint, format, test.
logging.getLogger(name)Leveled messages instead of print: debug, info, warning, error.
timeit / cProfileTime a snippet / see which functions take the time.
set and dict lookupsNear-instant membership tests; the commonest big speed-up.

Related courses