How to Create setup.py for Python Package: Simple Guide
To create a
setup.py file for a Python package, write a Python script using setuptools.setup() to define package metadata like name, version, and modules. This file helps you build and distribute your package easily.Syntax
The setup.py file uses the setuptools.setup() function to specify your package details.
- name: The package name.
- version: The package version.
- packages: List of Python packages to include.
- install_requires: List of dependencies.
- author: Package author name.
- description: Short package description.
python
from setuptools import setup, find_packages setup( name='your_package_name', version='0.1.0', packages=find_packages(), install_requires=[], author='Your Name', description='A short description of your package' )
Example
This example shows a simple setup.py for a package named mypackage with one dependency requests.
python
from setuptools import setup, find_packages setup( name='mypackage', version='0.0.1', packages=find_packages(), install_requires=['requests'], author='Jane Doe', description='Example package for demonstration' )
Output
running install
running bdist_egg
running egg_info
writing mypackage.egg-info\PKG-INFO
writing dependency_links to mypackage.egg-info\dependency_links.txt
writing requirements to mypackage.egg-info\requires.txt
writing top-level names to mypackage.egg-info\top_level.txt
reading manifest file 'mypackage.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'mypackage.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build\bdist.win-amd64\egg
copying build\lib\mypackage\__init__.py -> build\bdist.win-amd64\egg\mypackage
byte-compiling build\bdist.win-amd64\egg\mypackage\__init__.py to __init__.cpython-310.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying mypackage.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying mypackage.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying mypackage.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying mypackage.egg-info\requires.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying mypackage.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\mypackage-0.0.1-py3.10.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg'
Processing mypackage-0.0.1-py3.10.egg
removing 'c:\users\user\appdata\local\temp\pip-install-xyz\mypackage' (and everything under it)
creating c:\users\user\appdata\local\programs\python\python310\lib\site-packages\mypackage-0.0.1-py3.10.egg
Extracting mypackage-0.0.1-py3.10.egg to c:\users\user\appdata\local\programs\python\python310\lib\site-packages
mypackage 0.0.1 is already the active version in easy-install.pth
Installing mypackage-script.py script to c:\users\user\appdata\local\programs\python\python310\Scripts
Finished processing dependencies for mypackage==0.0.1
Common Pitfalls
Common mistakes when creating setup.py include:
- Forgetting to include
find_packages()or listing packages incorrectly, so your modules are not included. - Not specifying dependencies in
install_requires, causing missing packages on install. - Using an outdated
distutils.core.setupinstead ofsetuptools.setup. - Incorrect version format or missing required fields like
nameorversion.
python
from distutils.core import setup # Older approach, avoid setup( name='badpackage', version='1.0', packages=['mypkg'], # Might miss subpackages ) # Correct way: from setuptools import setup, find_packages setup( name='goodpackage', version='1.0', packages=find_packages() )
Quick Reference
Remember these tips when writing setup.py:
- Always use
setuptoolsandfind_packages()for automatic package discovery. - Specify dependencies in
install_requires. - Keep
nameandversionupdated for releases. - Test installation locally with
python setup.py installorpip install ..
Key Takeaways
Use setuptools.setup() in setup.py to define your package metadata and dependencies.
Include find_packages() to automatically find all your package modules.
List dependencies in install_requires to ensure they install with your package.
Avoid using distutils; setuptools is the modern standard.
Test your setup.py by installing your package locally before distribution.