Setuptools: A Comprehensive Overview

Setuptools is a powerful and widely-used library in Python that plays a crucial role in the packaging and distribution of Python projects. It is primarily used to create Python packages, manage dependencies, and facilitate the distribution of these packages. Setuptools is often employed in conjunction with other tools like wheel and twine to streamline the process of preparing a package for publishing.

Definition and Role in Python Packaging

Setuptools has historically been one of the primary tools for creating Python packages. It remains a significant player in the ecosystem as one of the available build backends for Python package builds. As a build backend, Setuptools is utilized by the Python build system to facilitate the creation of packages, particularly in conjunction with the 'wheel' package during the build_wheel step. When no other backend is specified, the 'build' tool defaults to using Setuptools.

For more details, refer to the Setuptools: A Comprehensive Overview.

Build System Backend Specification

To specify Setuptools as the build system backend, you need to configure your project with a pyproject.toml file. This file should include the following configuration:

[build-system]                             #1
requires = ["setuptools", "wheel"]         #2
build-backend = "setuptools.build_meta"    #3
#1 Opens a new TOML section
#2 List of package names as strings
#3 Dotted path to the object as a string

This configuration ensures that Setuptools, along with the 'wheel' package, is used to build your package. By explicitly specifying Setuptools as the backend, you lock in its use rather than relying on the default behavior of the 'build' tool.

For more information, see Build System Backend Specification.

Package Development and Distribution

Setuptools is not only a tool for building packages but also supports the development of command-line tools through entry points. This feature allows developers to map command names to functions, making the package's functionality accessible directly from the command line.

When building a package, Setuptools can create both source and binary distributions. A source distribution allows users to build the package on their platform, while a binary distribution is prebuilt for a specific platform, saving users the effort of building it themselves.

For an example of building an empty Python package, refer to Listing 3.1.

Using Setuptools in setup.py

The setup.py file is the heart of a Python package. It contains all the necessary information to build, package, and distribute the package. Below is an example of how setuptools is used in a setup.py file:

import setuptools

with open("README.md") as file:
   read_me_description = file.read()

setuptools.setup(
   name="unique_package_name",
   version="0.1",
   author="Your Name",
   author_email="your_email_address",
   description="This is a task management app.",
   long_description=read_me_description,
   long_description_content_type="text/markdown",
   url="https://github.com/ycui1/python_how_to",
   packages=setuptools.find_packages(),
   install_requires = ["streamlit"],
   classifiers=[
       "Programming Language :: Python :: 3",
       "License :: OSI Approved :: MIT License",
       "Operating System :: OS Independent",
   ],
   python_requires='>=3.10',
)

Key Arguments in setuptools.setup()

  • name: The name of the package.
  • version: The current version of the package.
  • author: The name of the package author.
  • author_email: The email address of the package author.
  • description: A short description of the package.
  • long_description: A detailed description of the package, often loaded from a README.md file.
  • long_description_content_type: The format of the long description, such as text/markdown.
  • url: The URL for the package's homepage or repository.
  • packages: A list of all Python import packages that should be included in the distribution package. This is often generated using setuptools.find_packages().
  • install_requires: A list of dependencies that are required for the package to work.
  • classifiers: A list of classifiers that provide some additional metadata about the package.
  • python_requires: Specifies the Python version requirement.

For more details, see Using Setuptools in setup.py.

Publishing a Package

To publish a package, you need three tools: setuptools, wheel, and twine. While setuptools is a built-in module, wheel and twine need to be installed separately. You can install or update these tools using the following command:

python3 -m pip install --upgrade setuptools wheel twine

This command ensures that you have the latest versions of these tools, which are essential for building and distributing your package.

For more information, refer to Publishing a Package.

| Book Title | Usage of setuptools | Technical Depth | Connections to Other Concepts | Examples Used | Practical Application |
|------------|---------------------|-----------------|-------------------------------|---------------|-----------------------|
| [Publishing Python Packages: Test, share, and automate your projects](https://www.manning.com/books/publishing-python-packages) | Discusses setuptools as a build backend for Python package builds, particularly in conjunction with the 'wheel' package. [more](https://livebook.manning.com/book/publishing-python-packages/chapter-3/1) | Provides detailed configuration examples using `pyproject.toml` to specify setuptools as the build system backend. [more](https://livebook.manning.com/book/publishing-python-packages/chapter-3/26) | Connects setuptools with the 'wheel' package and entry points for command-line tools. [more](https://livebook.manning.com/book/publishing-python-packages/chapter-4/92) | Includes examples of building both source and binary distributions, highlighting the importance of metadata. [more](https://livebook.manning.com/book/publishing-python-packages/chapter-3/listing--3-1) | Emphasizes practical steps for package development and distribution, including command-line tool support. [more](https://livebook.manning.com/book/publishing-python-packages/chapter-4/92) |
| [Python How-To](https://www.manning.com/books/python-how-to) | Highlights setuptools as essential for preparing a package for publishing, focusing on its use in `setup.py`. [more](https://livebook.manning.com/book/python-how-to/appendix-e) | Explains key arguments in `setuptools.setup()`, such as name, version, and dependencies. [more](https://livebook.manning.com/book/python-how-to/appendix-e) | Links setuptools with `wheel` and `twine` for publishing packages. [more](https://livebook.manning.com/book/python-how-to/appendix-e) | Provides a `setup.py` example demonstrating the use of `setuptools.find_packages()`. [more](https://livebook.manning.com/book/python-how-to/appendix-e) | Guides on installing and updating setuptools, wheel, and twine for package distribution. [more](https://livebook.manning.com/book/python-how-to/appendix-e) |

FAQ (Frequently asked questions)

What is the purpose of the setuptools module in Python?

Where is the setuptools module typically used?

What function does setuptools provide to identify packages for distribution?