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, orbdist_wheeldirectly:- Direct
setup.pyCLI invocations (python setup.py install,python setup.py sdist,python setup.py test) are deprecated and actively removed in modernsetuptools(v70+/v80+) because they bypass build isolation, dependency resolution, and standard.dist-metadata. - Always use standard PEP 517 frontends instead:
- Install locally (regular or editable):
python -m pip install .orpython -m pip install -e . - Build
.whland.tar.gzdistributions:python -m build(from thebuildpackage)
- Install locally (regular or editable):
- Direct
- Why
pyproject.tomlreplacedsetup.pyas the standard:- Because
setup.pyis arbitrary executable Python code, tools likepipor PyPI cannot inspect a package’s name, version, or dependencies without running untrusted code on your machine.pyproject.tomlstores metadata statically in TOML sopip,uv,hatch,poetry, andflitcan parse dependencies safely and reproducibly.
- Because
- Hybrid
pyproject.toml+ minimalsetup.pypattern:- When a package needs custom C/C++ compiler flags or Cython
ext_modules, keep all static metadata ([project],name,version,dependencies) inpyproject.tomland put only thesetup(ext_modules=[...])call insidesetup.py—setuptoolsautomatically merges both duringpip install .orpython -m build.
- When a package needs custom C/C++ compiler flags or Cython
- Editable installs (
pip install -e .) work withoutsetup.py:- Modern
setuptools(>=64) andpip(>=21.3) support PEP 660 editable installs (python -m pip install -e .) using solelypyproject.toml—no dummysetup.pyshim is required.
- Modern
requirements.txtvs.setup.py/pyproject.toml:pyproject.toml(dependencies) andsetup.py(install_requires) declare abstract, flexible version ranges (requests>=2.31) for reusable libraries/packages, whereasrequirements.txt(oruv.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. |
You must log in or register to comment.
