Lesson 20: Building and Packaging a Small Python Project

How to structure, package, and distribute your own Python application

Why Packaging Matters

Packaging allows you to share your Python code with others, install it on different systems, or publish it to PyPI. A well‑structured project is easier to maintain, test, and extend.

Project Structure

A typical Python project layout looks like this:

myproject/
    myproject/
        __init__.py
        core.py
    tests/
        test_core.py
    README.md
    pyproject.toml
    LICENSE

Creating the Package Code

# myproject/core.py

def greet(name):
    return f"Hello, {name}!"

Adding __init__.py

This file marks the folder as a Python package:

# myproject/__init__.py

from .core import greet

Using pyproject.toml

This file defines metadata and build settings for your package:

[project]
name = "myproject"
version = "0.1.0"
description = "A simple example project"
authors = [{name = "Your Name"}]

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

Adding a README

A README.md explains what your project does and how to use it.

Building the Package

Use the build tool:

pip install build
python -m build

This creates:

Installing Your Package Locally

pip install dist/myproject-0.1.0-py3-none-any.whl

Publishing to PyPI

Upload using twine:

pip install twine
twine upload dist/*

Versioning Your Project

Follow semantic versioning:

Why Packaging Matters

Next Steps

Now that you can build and package a Python project, you're ready to explore how to distribute applications in Lesson 21: Creating Executable Applications.

← Back to Lesson Index