Include file from outside the project folder in pyproject.toml using hatch build backend

773 Views Asked by At

Given the following folder structure:

-folder1
    -folder2
        -folder3
            -a.py
        -pyproject.toml
    -xyz.txt

And pyproject.toml

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "folder2"
version = "0.0.1"
description = "asd"

[tool.hatch.build.targets.wheel.force-include]
"../xyz.txt" = "folder3/xyz.txt"

Enter folder2 and run python -m build. According to https://hatch.pypa.io/latest/config/build/#forced-inclusion , it should generate a wheel file that contains folder2 with 2 files in it: a.py and xyz.txt, since

The force-include option allows you to select specific files or directories from anywhere on the file system that should be included and map them to the desired relative distribution path.. However, xyz.txt is not included. If I specify an absolute path to xyz.txt it works. However, relative path should also work according to the example shown at the forced-inclusion section: "../artifacts" = "pkg".

1

There are 1 best solutions below

0
Stuart Cardall On

Following the python.org packaging structure will avoid these problems.

  • See my pyproject.toml for how I package an examples directory which exists at the same level as the src directory:
├── dist
├── examples
├── LICENSE
├── pyproject.toml
├── README.md
├── src
└── tests
  • in particular:
[tool.hatch.build.targets.wheel.force-include]
"examples" = "distrobuilder_menu/examples"

[project]
name = "distrobuilder_menu"
  • Using this project layout with your pyproject.toml in the top level removes the need for relative includes.
  • Ensure your include path leads with {name}/dir so you package things within your module & not under site-packages.