What is setup.py?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
setup.py is the traditional build script for Python packages. It uses the setuptools library to define package metadata (name, version, author), dependencies, entry points, and installation instructions. When you run pip install . or python setup.py install, this file tells the build system how to package and install your project.
Basic setup.py
Key Fields
| Field | Purpose | Example |
name | Package name on PyPI | 'my-package' |
version | Semantic version | '1.2.3' |
packages | Python packages to include | find_packages() |
install_requires | Runtime dependencies | ['requests>=2.25'] |
entry_points | CLI commands | {'console_scripts': [...]} |
python_requires | Minimum Python version | '>=3.8' |
classifiers | PyPI metadata tags | ['License :: OSI Approved :: MIT License'] |
Common Commands
Entry Points (CLI Commands)
Define command-line tools that get installed with your package:
After installation, my-command is available in the terminal and calls main() from my_package/cli.py.
Extra Dependencies
Define optional dependency groups:
Package Data and Non-Python Files
Include data files like templates, configs, or static assets:
Modern Alternative: pyproject.toml
setup.py is being replaced by pyproject.toml (PEP 621), which uses a declarative format:
pyproject.toml is preferred for new projects because:
- It is declarative (no executable code)
- It supports multiple build backends (setuptools, flit, poetry, hatch)
- It consolidates tool configuration (
[tool.pytest],[tool.black], etc.)
setup.py vs setup.cfg vs pyproject.toml
| Feature | setup.py | setup.cfg | pyproject.toml |
| Format | Python script | INI-style | TOML |
| Dynamic logic | Yes | Limited | No |
| Build backends | setuptools only | setuptools only | Any |
| Recommended for new projects | No | No | Yes |
| PyPI upload | Yes | Yes | Yes |
Common Pitfalls
- Missing MANIFEST.in: When building source distributions (
sdist), non-Python files are excluded unless listed inMANIFEST.inor specified viapackage_data. - Version sync: Keeping the version in
setup.pyin sync with__version__in your package is error-prone. Usesetuptools-scmor single-source the version from your package. - find_packages scope:
find_packages()includes all Python packages it finds, including test directories. Usefind_packages(exclude=['tests', 'tests.*'])to exclude them. - install_requires vs requirements.txt:
install_requiresinsetup.pylists abstract dependencies (what your package needs).requirements.txtlists pinned versions (what to install in a specific environment). They serve different purposes. - Executable setup.py: Because
setup.pyis executable Python, it can contain arbitrary code that runs during installation. This is a security concern — prefer declarativepyproject.tomlfor new projects.
Summary
setup.pyis the traditional Python package build script usingsetuptools- It defines package metadata, dependencies, entry points, and installation behavior
- Use
pip install -e .for development installation (editable mode) - For new projects, prefer
pyproject.toml(PEP 621) as the declarative alternative - Use
find_packages()to automatically discover packages, excluding tests
Related reading
- What is setup.py?
- What is sklearn.cross_validation.cross_val_score
- What is sys.maxint in Python 3?
- What is TensorFlow Eager module for?
- What is tensorflow.compat.as_str?
- What is tensorflow.python.data.ops.dataset_ops._OptionsDataset?
- What is the '' symbol for in Python?
- What is the Bash equivalent of Python's pass statement
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.