Entry Points in pyproject.toml
Learn to declare entry points in pyproject.toml using the three supported tables. Avoid nested subtables and duplicate definitions to prevent installation errors.
On this page
The short answer
Entry points define how a Python package exposes CLI scripts and plugin mechanisms. The pyproject.toml spec provides three specific tables to manage these declarations correctly.
Introduction to Entry Points in pyproject.toml
Entry points define executable scripts and plugin interfaces for Python projects. The pyproject.toml file specifies these using dedicated tables under the project section. This formal structure ensures consistency across different build tools and installation environments. It allows packages to expose functionality without requiring users to import modules manually. The specification defines three specific tables for scripts and entry points. Each table serves a specific purpose in the installation workflow.
These tables support automatic script creation and plugin registration. Proper use simplifies distribution and installation by centralizing configuration.
Practical tip
Use project.scripts for CLI tools and project.gui-scripts for GUI tools. Keep all plugin groups in project.entry-points without nesting, as the specification requires this flat structure to ensure build backend compatibility.
Declaring Console Scripts with project.scripts
The project.scripts table maps command-line names to callable object references. Each key represents the script name users type in the terminal after installing the package. The value must be a string pointing to a module and function that accepts arguments. Build back-ends generate wrapper scripts that invoke these functions with command-line arguments. These scripts are placed in the user's PATH so they are globally accessible. Proper formatting ensures the script runs correctly on Windows and Unix systems.
Standard toolchains like setuptools or Hatch process this table to create executables. The function called receives standard arguments passed to the CLI.
Declaring GUI Scripts with project.gui-scripts
GUI scripts follow the same pattern but target graphical user interface applications. The project.gui-scripts table is distinct to signal that these entries open windows. Build tools handle them slightly differently depending on the operating system environment. This separation helps tools decide how to manage process focus and windowing. GUI scripts might not block the terminal like standard console scripts do. They allow applications to run in the background while providing visual feedback.
Use this section when your application requires a graphical interface rather than console output. The entry point function usually launches a main loop.
Using project.entry-points for Custom Groups
Custom groups use the project.entry-points table to define plugin discovery mechanisms. Each sub-table corresponds to a specific group name used by the host application. Keys and values inside follow the same name and object reference semantics as scripts. This allows third-party packages to register plugins that extend the core functionality. Host applications load these groups dynamically at runtime without hardcoding paths. Developers can add features by installing packages that register to these groups.
This mechanism enables extensible architectures. Plugins simply declare their group in this section to participate.
Avoiding Nested Subtables in Entry Points
The specification forbids nested subtables within the project.entry-points table. Groups must remain at a single level to avoid structural ambiguity during parsing. Build back-ends will raise errors if they detect nested keys where flat groups are expected. This constraint simplifies the implementation of tools that read these configurations. Flat structures are easier to validate and process automatically without complex recursion logic.
Do not create deeper levels under a group name. Each group maps directly to its entry point items.
Understanding Ambiguity Errors with Duplicate Definitions
Defining console_scripts or gui_scripts as sub-tables within [project.entry-points] creates ambiguity. Build back-ends MUST raise an error when detecting such duplicate definitions in metadata. This prevents conflicts where tools cannot determine which definition should generate the actual script. Users must choose one table for console scripts to ensure consistent behavior. The specification explicitly prohibits this to maintain clear separation of concerns and avoid confusion.
If a name appears in both locations the build fails. Check your configuration to resolve the conflict.
Best Practices for Entry Point Naming and Object Reference
Entry point names serve as keys and object references as values in these tables. Use the format package.module:callable to ensure the target is reachable after installation. Keys cannot contain invalid characters that would break script generation on different platforms. Values must match the object path exactly to avoid import errors at runtime. Tools often normalize names but the original reference remains critical for finding the correct entry point.
Ensure all referenced modules exist within your package. Test the entry point after installation to verify it works.
Integration with Build Back-ends and Installation Tools
Installation tools validate these tables before creating distribution files or entry points. They check for required fields and correct types to ensure metadata compliance. Errors in configuration stop the build process and inform the developer of specific issues. Adhering to these rules ensures packages install correctly across different systems. Proper validation prevents users from encountering runtime failures later or installation issues.
Modern back-ends like hatchling or setuptools report clear errors for invalid entry points. Check metadata validation errors reported by build back-ends when your scripts fail to generate.
Things to check
- Verify that [project.scripts] and [project.gui-scripts] are flat tables
- Ensure [project.entry-points] does not contain nested subtables
- Check that console_scripts or gui_scripts are not defined as sub-tables within [project.entry-points]
- Confirm all entry point object references follow the module:callable format
- Confirm all entry point object references exist in the source
- Test installed scripts after building the distribution
Where this applies
This specification applies to pyproject.toml configuration as defined in the packaging spec. Custom build back-ends may have additional requirements or validation rules.