Demystifying Setup.py: The Blueprint Behind Python Packages 📦
Ever wondered what makes 'pip install' work? Let's break down the mechanics behind setup.py 📦
At its core, setup.py is a Python script that defines your package's identity card. It contains essential metadata about your package - from its name and version to dependencies and distribution settings.
Here's what a basic setup.py looks like:
setup(
name="my-awesome-package",
version="1.0.0",
author="Your Name",
description="A short description",
)
Here are key techniques that make package distribution seamless:
- Smart Package Discovery:
setup(
packages=find_packages(exclude=['tests*']),
# Include non-Python files
package_data={
'mypackage': ['data/*.csv', 'models/*.pkl']
}
)
Pro tip: Don't forget to include init.py in all packages you want to distribute!
- Environment-Specific Dependencies:
setup(
install_requires=[
'numpy>=1.20.0',
'scipy; python_version>="3.8"',
'pandas<2.0; python_version<"3.8"'
]
)
- Package Structure:
pythonCopysetup(
packages=find_packages(exclude=['tests*']),
# Include non-Python files
package_data={
'mypackage': ['data/*.csv', 'models/*.pkl']
}
)
- Distribution Settings:
Determines what gets included in your wheel/distribution files
Controls how your package is built and installed
Manages Python version compatibility
When you're ready to distribute, simply run: python3 setup.py bdist_wheel
This creates a wheel file - a binary distribution format that makes installation faster and more reliable.
🔍 Want to dive deeper? Here are resources I found useful:
Here’s a video that I had used long ago to understand the setup.py file better:
https://www.youtube.com/watch?v=QgZ7qv4Cd0Y
The source documentation: