setuptools

repository·main·Indexed 25 days ago

https://github.com/pypa/setuptools

A core Python library for packaging and distributing projects, serving as an extensible build backend with support for C/C++ extension modules. It provides infrastructure for defining build requirements, managing dependencies, and creating installation artifacts, including support for PEP 517 via setuptools.build_meta.

Tokens
31.6K
Snippets
63
Records
230
Agent score
83%

What's inside setuptools

  1. Overview of Building and Distributing Packages with Setuptools

    main

    To share a Python library or program, you must build a distribution package. This involves adding metadata and configuration files that instruct setuptools on how to build the distribution and assist installers like pip during the installation process.

    Note that setuptools acts as a build backend. While setuptools handles the build logic, users typically interact with it through front-end tools like pip or build. To use setuptools as a build backend, you must explicitly create a pyproject.toml file.

  2. Understand Data Files Support in setuptools

    main

    In setuptools, "data files" refers to non-Python files that are installed alongside Python modules and packages when a distribution is installed via a wheel. These files are typically intended for use at runtime by the package itself or to influence the behavior of other packages or systems.

    Setuptools provides three primary ways to specify which non-Python files should be included in your packages, primarily focusing on including them inside the package directory.

  3. Understand default file inclusion in distributions

    main

    By default, setuptools automatically includes several types of files in a source distribution (sdist). These include:

    • Python Modules: Files implied by py-modules and packages in pyproject.toml, setup.cfg, or setup.py.
    • C Source Files: Files mentioned in ext_modules or libraries arguments in setup().
    • Test Files: Files matching tests/test*.py or test/test*.py.
    • Scripts: Files specified by scripts-files in pyproject.toml or scripts in setup.py/setup.cfg.
    • Data Files: All files specified by package-data and data-files.
    • License Files: Files specified by license_file or license-files. If not specified, setuptools includes files matching LICEN[CS]E*, COPYING*, NOTICE*, or AUTHORS**.
    • Project Metadata: pyproject.toml, setup.cfg, setup.py, MANIFEST.in, and README files (README.txt, README.rst, README.md).

    Note on Type Information (Experimental): Since v69.0.0, setuptools attempts to include .pyi and py.typed files by default if they are inside a package directory. You can opt out using exclude-package-data.

  4. Set build-system requirements for PEP 639 support

    main

    SPDX expressions and the license-files field require setuptools v77.0.0 or later. Because build frontends like pip and build often use isolated environments, you should specify this requirement in your [build-system] table to ensure the build backend can parse the new license format.

    [build-system]
    requires = ["setuptools>=77"]
    build-backend = "setuptools.build_meta"
    
    [project]
    name = "my_package"
    license = "Apache-2.0"
    license-files = ["LICENSE"]
  5. Configure a src/ layout in setup.cfg

    main

    If your Python source code is located in a subdirectory (commonly src/), you must configure package_dir and packages.find in your setup.cfg to ensure correct discovery.

    To map the root package directory to the src folder, use package_dir = =src. The =src syntax is parsed as {"": "src"}, where the empty string key indicates that all packages are contained within that directory.

    In the [options.packages.find] section, set where=src to match the directory specified in package_dir.

    [options]
    package_dir =
        =src
    packages = find:
    
    [options.packages.find]
    where = src
  6. Add new setup() arguments via entry points

    main

    To add a new keyword argument to the setup() function, define an entry point in the distutils.setup_keywords group. The entry point must point to a validation function that accepts three arguments: the Distribution object, the attribute name, and the attribute value.

    Note: Adding arguments to setup() is discouraged as it is only supported through imperative execution and not through declarative configuration. The default value for these arguments is always None.

  7. Add custom metadata files via egg_info.writers

    main

    To allow plugins to include application-specific metadata in the .egg-info directory, use the egg_info.writers entry point group.

    A writer function must accept three arguments:

    1. The egg_info command instance (cmd).
    2. The basename of the file to write (e.g., foo_bar.txt).
    3. The full filename to be written.

    To ensure compatibility with dry_run settings and proper logging, use the command object's file methods: cmd.write_file(), cmd.delete_file(), or cmd.write_or_delete_file().

    # setup.cfg
    [options.entry_points]
    egg_info.writers =
        foo_bar.txt = setuptools.command.egg_info:write_arg
  8. Configure Console Scripts in setup.cfg and setup.py

    main

    If you are not using pyproject.toml, you can define console scripts using setup.cfg or setup.py.

    # setup.cfg
    [options.entry_points]
    console_scripts =
        hello-world = timmins:hello_world
    # setup.py
    from setuptools import setup
    
    setup(
        # ...
        entry_points={
            'console_scripts': [
                'hello-world = timmins:hello_world',
            ]
        }
    )
  9. Use the setuptools logo and design elements

    main

    The setuptools logo is designed to be used primarily over a white or light background. Depending on your use case, you can use the following variations:

    • Standard: Over a white/light background.
    • negative: For dark backgrounds (e.g., dark mode). Swap the background color (#FFFFFF) with the design's blue color (#336790).
    • monochrome: For black and white media. Use a completely black or white version.
    • banner: Use the symbol and text alongside each other depending on available space.

    Design Specifications:

    • Blue: #336790 (dark pastel azure)
    • Yellow: #E5B62F (pale orange)
    • Typography: Monoid typeface (open source webfont).