Codzy

Pro Python: projects, environments and versions

A real Python project has a structure: a pyproject.toml file (name, version, dependencies), the code in src/, the tests in tests/. It's installed in a virtual environment (python -m venv .venv) so that each project has its own library versions, with no conflicts.

Versions follow semantic versioning: MAJOR.MINOR.PATCH. Watch out for the trap: compared as text, "2.10" < "2.9"! You need to compare tuples of integers: (2, 10) > (2, 9). That's how pip knows whether to upgrade.

# A typical project layout
#
# my_tool/
#   pyproject.toml      <- name, version, dependencies
#   src/my_tool/__init__.py
#   src/my_tool/core.py
#   tests/test_core.py
#
# Install it in a virtual environment:
#   python -m venv .venv
#   pip install -e .

requirements = """requests>=2.31
rich==13.7.1
# dev tools
pytest>=8.0"""
for line in requirements.splitlines():
    if line and not line.startswith("#"):
        print(line)

Start the lesson