requirements.txt vs setup.py
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
requirements.txt and setup.py solve related but different problems. A requirements file tells pip what to install into an environment, while setup.py historically describes a Python package itself: its metadata, its install dependencies, and how it should be built.
That distinction still matters, even though modern Python packaging now prefers pyproject.toml over directly running setup.py. If you confuse environment requirements with package metadata, dependency management gets messy fast.
What requirements.txt Is For
A requirements file is an environment installation list. It is commonly used for:
- local development environments
- deployment environments
- CI jobs
- repeatable application installs
Example:
Install it with:
The pip documentation defines requirements files as a list of items to be installed. That makes them a natural fit for applications where you want a specific environment assembled in a predictable way.
What setup.py Is For
setup.py belongs to Python packaging rather than plain environment setup. It describes a package that other people or projects can install.
A classic example looks like this:
This tells packaging tools things such as:
- package name
- version
- package contents
- runtime dependencies
- optional extras
That is very different from saying "install these exact packages into this machine right now."
The Most Practical Rule of Thumb
Use requirements.txt for an environment. Use setup.py or, more commonly today, pyproject.toml for a distributable package.
That means:
- an application repository often has
requirements.txt - a reusable library often has package metadata with
install_requiresorproject.dependencies - many projects have both
For example, a library may declare broad runtime compatibility in package metadata, while the development repo uses a tighter requirements file for tests and linting.
Modern Python Packaging Note
The important modern nuance is that python setup.py install is now considered a deprecated practice by setuptools. The packaging role still exists, but current projects typically express it in pyproject.toml instead of relying on setup.py as the main entry point.
A modern equivalent looks like this:
So the old comparison is still conceptually useful, but in new projects you should mentally read it as requirements.txt versus package metadata, often in pyproject.toml.
A Common Real-World Layout
Many healthy repositories separate concerns like this:
- '
pyproject.tomlfor package metadata and runtime dependencies' - '
requirements.txtfor a deployable application environment' - '
requirements-dev.txtor an extra such as.[dev]for test and lint tooling'
That layout makes it obvious which dependencies are part of the package contract and which ones only support development workflows.
Common Pitfalls
- Using
pip freezeoutput as a library's runtime dependency list. That usually pins far more than the library actually needs. - Expecting
requirements.txtto publish package metadata. It does not describe your package to packaging tools. - Putting development-only tools into runtime package dependencies.
- Assuming
setup.pyis the modern way to invoke packaging commands. The script can still exist, but directpython setup.py ...usage is deprecated. - Forgetting that applications and libraries often need different dependency strategies.
Summary
- '
requirements.txtis for installing a concrete environment withpip install -r.' - '
setup.pyhistorically describes a Python package and its dependencies.' - In modern projects,
pyproject.tomloften replacessetup.pyas the preferred packaging configuration. - Applications often rely on requirements files, while reusable libraries rely on package metadata.
- Keeping environment pins separate from package metadata makes Python dependency management much easier to reason about.
Related reading
- Requiring tensorflow with Python 2.7.11 occurs ImportError
- Reset kafka LAG (change offset) within consumer group in Kafka-python
- Resetting generator object in Python
- Reshape your data either using array.reshape-1, 1 if your data has a single feature or array.reshape1, -1 if it contains a single sample
- Residual plot for residual vs predicted value in Python
- Result of GridSearchCV as table
- Results not reproducible with Keras and TensorFlow in Python
- Retrieve list of tasks in a queue in Celery
.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.