Easily download, build, install, upgrade, and uninstall Python packages
—
Setuptools command system providing extensible build, installation, and distribution functionality through specialized command classes for every aspect of Python package lifecycle management. These commands can be executed via setup.py or used programmatically.
Commands for building Python packages and their components, including modules, extensions, and libraries.
class build(Command):
"""
Enhanced build command that coordinates all build operations.
Builds all components of the package including Python modules,
C extensions, and data files.
Options:
- build-base: Base directory for build files
- build-lib: Directory for built Python modules
- build-scripts: Directory for built scripts
- build-temp: Directory for temporary build files
- debug: Compile with debugging information
- force: Force rebuild of everything
- executable: Path to Python interpreter
"""
class build_py(Command):
"""
Enhanced Python module builder with package discovery.
Builds pure Python modules and packages, handling package
data and namespace packages.
Options:
- build-lib: Directory to build modules
- compile: Compile .py to .pyc files
- optimize: Optimization level for .pyc files
- force: Force rebuild
"""
class build_ext(Command):
"""
Enhanced C/C++ extension builder with Cython support.
Builds C/C++ extension modules with enhanced features like
Cython compilation and improved error handling.
Options:
- build-lib: Directory for built extensions
- build-temp: Directory for temporary files
- inplace: Build extensions in-place
- include-dirs: Include directories
- library-dirs: Library directories
- libraries: Libraries to link against
- debug: Build with debug information
- force: Force rebuild
- compiler: Compiler to use
- swig: Path to SWIG executable
- swig-opts: SWIG options
- user: Install to user site-packages
- parallel: Number of parallel build jobs
"""
class build_clib(Command):
"""
C library builder for building static libraries.
Builds C/C++ libraries that can be used by extensions
in the same package.
Options:
- build-clib: Directory for built libraries
- build-temp: Directory for temporary files
- include-dirs: Include directories
- libraries: Library definitions
- debug: Build with debug information
- force: Force rebuild
- compiler: Compiler to use
"""Commands for creating distributable packages in various formats.
class bdist_wheel(Command):
"""
Build wheel distribution (.whl files).
Creates binary distribution wheels that can be installed
quickly without compilation.
Options:
- bdist-dir: Temporary directory for building
- dist-dir: Directory for final wheel files
- skip-build: Skip build step
- relative: Force relative paths in wheel
- owner: Owner for files in wheel
- group: Group for files in wheel
- universal: Create universal wheel
- python-tag: Python tag for wheel filename
- plat-name: Platform name for wheel
"""
class bdist_egg(Command):
"""
Build egg distribution (legacy format).
Creates .egg files for distribution. This format is
largely superseded by wheels.
Options:
- bdist-dir: Temporary directory for building
- dist-dir: Directory for final egg files
- skip-build: Skip build step
- keep-temp: Keep temporary files
- exclude-source-files: Don't include source in egg
"""
class sdist(Command):
"""
Enhanced source distribution builder.
Creates source distributions (.tar.gz) with improved
file discovery and MANIFEST.in support.
Options:
- template: MANIFEST template file
- manifest: MANIFEST file
- use-defaults: Include default files
- prune: Remove files/directories
- manifest-only: Only regenerate MANIFEST
- force-manifest: Force MANIFEST regeneration
- formats: Archive formats to create
- keep-temp: Keep temporary directory
- dist-dir: Directory for distributions
- owner: Owner for files
- group: Group for files
"""
class bdist_rpm(Command):
"""
Build RPM distribution for Red Hat-based systems.
Creates .rpm packages for installation on Red Hat,
CentOS, Fedora, and similar systems.
Options:
- bdist-base: Base directory for building
- rpm-base: Base directory for RPM building
- dist-dir: Directory for final RPM files
- python: Python executable path
- fix-python: Fix Python shebangs
- spec-only: Only create .spec file
- source-only: Only create source RPM
- binary-only: Only create binary RPM
- use-bzip2: Use bzip2 compression
- distribution-name: Distribution name
- group: RPM package group
- release: RPM release number
- serial: RPM serial number
- vendor: RPM vendor
- packager: RPM packager
- doc-files: Documentation files
- changelog: RPM changelog
- icon: RPM icon file
- provides: RPM provides
- requires: RPM requires
- conflicts: RPM conflicts
- build-requires: RPM build requires
- obsoletes: RPM obsoletes
- keep-temp: Keep temporary files
- no-keep-temp: Don't keep temporary files
- use-rpm-opt-flags: Use RPM optimization flags
- no-autoreq: Disable automatic dependency detection
- force-arch: Force architecture
"""Commands for installing packages and their components in various modes.
class install(Command):
"""
Enhanced install command with improved path handling.
Installs the package with setuptools enhancements like
automatic script generation and entry point handling.
Options:
- home: Install to specific home directory
- prefix: Install prefix
- user: Install to user site-packages
- root: Install root directory
- install-base: Base installation directory
- install-platbase: Platform-specific installation base
- install-lib: Installation directory for modules
- install-platlib: Installation directory for platform-specific modules
- install-purelib: Installation directory for pure Python modules
- install-headers: Installation directory for headers
- install-scripts: Installation directory for scripts
- install-data: Installation directory for data files
- compile: Compile .py files
- optimize: Optimization level
- force: Force installation
- skip-build: Skip build step
- record: Record installed files
- single-version-externally-managed: Single version mode
"""
class develop(Command):
"""
Development installation (editable install).
Installs the package in development mode where changes
to source files are immediately available without reinstall.
Options:
- install-dir: Installation directory
- script-dir: Script installation directory
- exclude-scripts: Don't install scripts
- always-copy: Always copy files (don't link)
- always-unzip: Always unzip eggs
- user: Install to user site-packages
- egg-path: Path to egg-link file
- multi-version: Multi-version mode
- upgrade: Upgrade existing installation
- uninstall: Uninstall development installation
"""
class easy_install(Command):
"""
Easy install command for dependency installation.
Installs packages and their dependencies with automatic
dependency resolution.
Options:
- zip-ok: Allow zip files
- multi-version: Multi-version mode
- upgrade: Upgrade existing packages
- install-dir: Installation directory
- script-dir: Script installation directory
- exclude-scripts: Don't install scripts
- always-copy: Always copy files
- index-url: PyPI index URL
- find-links: Additional package locations
- build-directory: Build directory
- optimize: Optimization level
- record: Record installed files
- always-unzip: Always unzip eggs
- site-dirs: Additional site directories
- editable: Editable installation
- no-deps: Don't install dependencies
- allow-hosts: Allowed download hosts
- local-snapshots-ok: Allow local snapshots
- user: Install to user site-packages
"""
class install_lib(Command):
"""Install Python modules and packages."""
class install_scripts(Command):
"""Install executable scripts."""
class install_egg_info(Command):
"""Install egg metadata files."""Commands for generating and managing package metadata.
class egg_info(Command):
"""
Generate egg metadata.
Creates .egg-info directory with package metadata
including dependencies, entry points, and file lists.
Options:
- egg-base: Base directory for egg-info
- tag-build: Tag to append to version
- tag-date: Add date tag to version
- no-date: Don't add date tag
- tag-svn-revision: Add SVN revision to version
- no-svn-revision: Don't add SVN revision
"""
class dist_info(Command):
"""
Generate dist-info metadata (PEP 376).
Creates .dist-info directory with standardized
package metadata for modern Python packaging.
Options:
- egg-base: Base directory for dist-info
- tag-build: Tag to append to version
- tag-date: Add date tag to version
"""
class editable_wheel(Command):
"""
Build editable wheels for PEP 660 compliance.
Creates wheel files that install packages in editable
mode following PEP 660 standards.
Options:
- wheel-dir: Directory for wheel files
- build-number: Build number for wheel
"""Commands for managing setuptools configuration and command options.
class alias(Command):
"""
Define command aliases.
Create shortcuts for commonly used command combinations.
Options:
- global-config: Store in global configuration
- user-config: Store in user configuration
- filename: Configuration file to use
"""
class saveopts(Command):
"""
Save command options to configuration file.
Stores command options for reuse in future runs.
Options:
- global-config: Save to global configuration
- user-config: Save to user configuration
- filename: Configuration file to use
"""
class setopt(Command):
"""
Set command options in configuration file.
Directly set option values in configuration files.
Options:
- global-config: Set in global configuration
- user-config: Set in user configuration
- filename: Configuration file to use
"""
class rotate(Command):
"""
Rotate old distribution files.
Remove old distribution files, keeping only the most
recent versions.
Options:
- match: Pattern for files to rotate
- dist-dir: Directory containing distributions
- keep: Number of files to keep
"""# Build commands
python setup.py build
python setup.py build_py
python setup.py build_ext --inplace
# Distribution commands
python setup.py sdist
python setup.py bdist_wheel
python setup.py bdist_egg
# Installation commands
python setup.py install
python setup.py develop
python setup.py install --user
# Metadata commands
python setup.py egg_info
python setup.py dist_info
# Configuration commands
python setup.py alias --global-config test "pytest tests/"
python setup.py saveopts build --user-configfrom setuptools import setup, Command
import subprocess
import os
class TestCommand(Command):
"""Custom test command."""
description = 'Run tests with pytest'
user_options = [
('test-args=', 'a', 'Arguments to pass to pytest'),
('coverage', 'c', 'Run with coverage'),
]
def initialize_options(self):
self.test_args = ''
self.coverage = False
def finalize_options(self):
if self.test_args:
self.test_args = self.test_args.split()
else:
self.test_args = []
def run(self):
cmd = ['pytest']
if self.coverage:
cmd.extend(['--cov=src', '--cov-report=html'])
cmd.extend(self.test_args)
self.announce(f'Running: {" ".join(cmd)}')
subprocess.check_call(cmd)
setup(
name='my-package',
cmdclass={
'test': TestCommand,
},
)from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
import numpy
class CustomBuildExt(build_ext):
"""Custom build_ext with additional features."""
def build_extensions(self):
# Custom logic before building
for ext in self.extensions:
ext.include_dirs.append(numpy.get_include())
# Call parent build
super().build_extensions()
extensions = [
Extension(
'my_package.fast_module',
sources=['src/fast_module.c'],
extra_compile_args=['-O3'],
)
]
setup(
name='my-package',
ext_modules=extensions,
cmdclass={
'build_ext': CustomBuildExt,
},
)# Initial development setup
python setup.py develop
# Build C extensions in-place for development
python setup.py build_ext --inplace
# Run tests during development
python setup.py test
# Create source distribution for release
python setup.py sdist
# Create wheel distribution
python setup.py bdist_wheel
# Upload to PyPI (with twine)
twine upload dist/*# Chain multiple commands
python setup.py build bdist_wheel
# Build and install in one step
python setup.py build install
# Create both source and wheel distributions
python setup.py sdist bdist_wheel# Set up command aliases
python setup.py alias --global-config build_all "build bdist_wheel"
python setup.py alias --user-config test "pytest tests/ -v"
# Save build options
python setup.py saveopts build_ext --user-config --include-dirs=/usr/local/include
# Use saved configuration
python setup.py build_ext # Uses saved options automaticallyInstall with Tessl CLI
npx tessl i tessl/pypi-setuptools