Split code across files with imports, use Python’s standard library, and install third-party packages with pip — tracked in pyproject.toml.
Why: a module is a file of reusable code. Python’s standard library ships with hundreds — for dates, maths, JSON, random numbers, and more — ready to import with no installation.
import math
print(math.sqrt(16)) # 4.0
# import just the part you need
from random import choice
print(choice(['a', 'b', 'c']))
# rename a long module
import datetime as dt
print(dt.date.today())Why: any .py file you write is a module. Import from it by its file name (without .py) to reuse functions across files and keep each file focused.
# file: helpers.py
def shout(text):
return f'{text.upper()}!'# file: main.py
from helpers import shout
print(shout('hello')) # HELLO!Why: when a file is imported, Python runs it top to bottom. This guard holds back code that should only run when the file is launched directly — not when it is imported as a module.
# file: helpers.py
def shout(text):
return f'{text.upper()}!'
if __name__ == '__main__':
# only runs when you do: python helpers.py
print(shout('test'))Why: pip downloads third-party packages from PyPI (the Python package index) into your active virtual environment. requests is a popular package for making HTTP calls.
Make sure your virtual environment is active, then:
pip install requestspip listpip install --upgrade requestsWhy: a project needs to record exactly which packages it depends on so anyone can reinstall them. The modern home for that is pyproject.toml. Newer tools like uv create and manage it for you and install far faster than pip.
# pyproject.toml
[project]
name = "my-app"
version = "0.1.0"
dependencies = [
"requests>=2.31",
"rich>=13.0",
]