setup.py is the legacy Python build-script entry point used by setuptools to declare package metadata (name, version, dependencies, console scripts) and run build/install commands—now largely replaced by declarative pyproject.toml files (PEP 517 / 518 / 621), and retained today primarily when compiling custom C/C++/Cython extensions (ext_modules) or maintaining backward compatibility with older tooling.

Basic Example

# 1. Modern Standard (pyproject.toml — recommended for all new Python projects):
[build-system]
requires = ["setuptools>=68.0"]
build-backend = "setuptools.build_meta"

[project]
name = "my-cli-tool"
version = "0.1.0"
description = "A sample Python package"
requires-python = ">=3.10"
dependencies = ["requests>=2.31"]

[project.scripts]
my-tool = "my_cli_tool.cli:main"
# 2. Legacy / Dynamic Build Script (setup.py — only needed for C extensions or dynamic hooks):
from setuptools import Extension, find_packages, setup

setup(
    name="my-cli-tool",
    version="0.1.0",
    packages=find_packages(where="src"),
    package_dir={"": "src"},
    install_requires=["requests>=2.31"],
    entry_points={
        "console_scripts": ["my-tool=my_cli_tool.cli:main"],
    },
    # Example reason to still use a minimal setup.py alongside pyproject.toml:
    ext_modules=[Extension("my_cli_tool._fast", sources=["src/my_cli_tool/_fast.c"])],
)

Key Details & Edge Cases

  • Never invoke python setup.py install, develop, or bdist_wheel directly:
    • Direct setup.py CLI invocations (python setup.py install, python setup.py sdist, python setup.py test) are deprecated and actively removed in modern setuptools (v70+/v80+) because they bypass build isolation, dependency resolution, and standard .dist-info metadata.
    • Always use standard PEP 517 frontends instead:
      • Install locally (regular or editable): python -m pip install . or python -m pip install -e .
      • Build .whl and .tar.gz distributions: python -m build (from the build package)
  • Why pyproject.toml replaced setup.py as the standard:
    • Because setup.py is arbitrary executable Python code, tools like pip or PyPI cannot inspect a package’s name, version, or dependencies without running untrusted code on your machine. pyproject.toml stores metadata statically in TOML so pip, uv, hatch, poetry, and flit can parse dependencies safely and reproducibly.
  • Hybrid pyproject.toml + minimal setup.py pattern:
    • When a package needs custom C/C++ compiler flags or Cython ext_modules, keep all static metadata ([project], name, version, dependencies) in pyproject.toml and put only the setup(ext_modules=[...]) call inside setup.py—setuptools automatically merges both during pip install . or python -m build.
  • Editable installs (pip install -e .) work without setup.py:
    • Modern setuptools (>=64) and pip (>=21.3) support PEP 660 editable installs (python -m pip install -e .) using solely pyproject.toml—no dummy setup.py shim is required.
  • requirements.txt vs. setup.py / pyproject.toml:
    • pyproject.toml (dependencies) and setup.py (install_requires) declare abstract, flexible version ranges (requests>=2.31) for reusable libraries/packages, whereas requirements.txt (or uv.lock) pins exact reproducible hashes/versions (requests==2.32.3) for deploying a specific application environment.

Comparison of Python Packaging Configuration & Build Files

File / Mechanism Format Purpose Status in Modern Python (3.11+) Best Used When
pyproject.toml (Recommended) Static TOML (PEP 621) Declares build backend (setuptools, hatchling, maturin), package metadata, dependencies, and CLI entry points. Current Standard All new Python libraries, CLI tools, and installable applications.
setup.py Executable Python script Imperative setuptools.setup(...) configuration and custom build hooks. Legacy / Specialized (never run python setup.py ... directly) Compiling C/C++/Cython ext_modules or maintaining compatibility with pre-PEP 517 workflows.
setup.cfg Static INI config Intermediate declarative alternative to setup.py before pyproject.toml (PEP 621) existed. Superseded by pyproject.toml Legacy setuptools projects that haven’t migrated INI keys to pyproject.toml yet.
requirements.txt / uv.lock Flat text / lockfile Pins exact dependency versions for reproducible environment deployments. Standard for App Deployment (complement to pyproject.toml) Docker images, CI pipelines, and deploying web services or data pipelines.