Skip to main content

Hatch build hook plugin that writes the project name to a file

Project description

hatch-project-name

CI/CD CI - Test CD - Build
Package PyPI - Version PyPI - Python Version
Meta Hatch project uv Ruff types - Mypy License - MIT

This provides a build hook plugin for Hatch that writes the project name defined in pyproject.toml to a file.

Table of Contents

Configuration

The build hook plugin name is project-name.

  • pyproject.toml

    [project]
    name = "my-project"
    
    [tool.hatch.build.hooks.project-name]
    dependencies = ["hatch-project-name"]
    name-file = "src/my_project/_name.py"
    
  • hatch.toml

    [project]
    name = "my-project"
    
    [build.hooks.project-name]
    dependencies = ["hatch-project-name"]
    name-file = "src/my_project/_name.py"
    

Building the project will generate the file

  • _name.py

    project_name = __project_name__ = distribution_name = __distribution_name__ = "my-project"
    

[!NOTE] This file is generated and should not be committed to version control. Remember to add it to your .gitignore. The plugin will automatically include it in your project’s built sdists and wheels.

Now you can import the project name:

from my_project import project_name

print(project_name)

Build hook options

Option Type Default Description
name-file str REQUIRED The relative path to the Python file that gets updated with the project name.

Editable installs

The name file is only updated upon install or build. Thus the name in an editable install (Hatch's dev mode) will be incorrect if the name is changed in pyproject.toml and the project is not rebuilt.

Rationale

The Hatch project name is a required field defined in the [project] table in pyproject.toml:

[project]
name = "my-project"

The [project] table specifies the project’s core metadata. The name project metadata field corresponds to the distribution package Name.

A distribution package is a piece of software that you can install. Most of the time, this is synonymous with “project”. When you type pip install pkg, or when you write dependencies = ["pkg"] in your pyproject.toml, pkg is the name of a distribution package. [...]

Most of the time, a distribution package provides one single import package (or non-package module), with a matching name. For example, pip install numpy lets you import numpy.

However, this is only a convention. PyPI and other package indices do not enforce any relationship between the name of a distribution package and the import packages it provides. [...]

Source: Distribution package vs. import package

The project/distribution name might be needed in code for various reasons. One might be that you are using the importlib.metadata standard library module to access project metadata. For instance:

from importlib import metadata

distribution_name = "my-project"
version = metadata.version(distribution_name)

Or you might simply want to log the name.

project_name = "my-project"
print(f"Project name: {project_name}")

The Python standard library does not provide a standardized function to programmatically access the current project/distribution name that works without limitations. This is because, as quoted above, no relationship is enforced between the name of a distribution package and the importable import packages it provides. You can of course just hardcode the project/distribution name as in the examples above, but this is not ideal since it is preferable to maintain pyproject.toml as the single source of truth for your project metadata.

The following method (originally posted here) is the best you can achieve with the standard library importlib.metadata module, but it has important limitations.

Say one top-level module or import package name in your project is my_tool. Then you can do:

from importlib import metadata

def get_project_name() -> str:
    pkg_name = "my_tool"
    pkg_to_dists = metadata.packages_distributions()
    return pkg_to_dists[pkg_name][0]

This uses importlib.metadata.packages_distributions() to map one of the top-level names to the project/distribution name.

This method has the following limitations:

  1. It does not work for namespace packages as the top-level namespace will map to potentially multiple distributions.
  2. It does not work with an editable install (unless you are using setuptools, see more details here and here). The main reason is that importlib.metadata "operates on third-party distribution packages installed into Python’s site-packages directory".

Alternatively, Hatch provides a CLI command hatch project metadata name that outputs the project name. However, this is cumbersome to use in Python code as you need to use something like subprocess.run to invoke the command in a working directory that contains the project source code. It additonally requires that Hatch is installed.

An even better alternative then is to generate a file containing the project name on-the-fly and include it in the distribution—and this is exactly what the hatch-project-name build hook does! Since the file is generated, you should exclude it from version control. This maintains pyproject.toml as the source of truth for the project name. Furthermore, Python project templates built with tools like cookiecutter and Copier will not need to parameterize the project name in Python source files.

License

hatch-project-name is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hatch_project_name-1.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hatch_project_name-1.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file hatch_project_name-1.0.0.tar.gz.

File metadata

  • Download URL: hatch_project_name-1.0.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for hatch_project_name-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e02afc0e1acb1917edf1bc426d62a6b15eccbb01b06529e4fae1e2da7de579da
MD5 8dc42893f43616e9412ce9f125fbade8
BLAKE2b-256 936b4b88f2112f15fb668d4b2d80dcdf11a6c89bac5c3a90c52b95ee7e5779e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for hatch_project_name-1.0.0.tar.gz:

Publisher: build.yml on valentinoli/hatch-project-name

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hatch_project_name-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hatch_project_name-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8895a066b660c1baf8de1a97eec466ce3adc2f6c2d462df6bd792645f69258e0
MD5 2b4965394dce61bff7d2efa3eb6d41f5
BLAKE2b-256 9dd54c8be7c1149ba306d39b54b818a9b387249c007cbc0731e216fa736d96a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for hatch_project_name-1.0.0-py3-none-any.whl:

Publisher: build.yml on valentinoli/hatch-project-name

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page