pythonsetuptoolspyproject.toml

How to select packaging files in pyproject.toml?


I am trying to build a python package. I want the build package to contain only the necessary files.

I make the build with following command: python -m build

My package files structure looks more or less like this:

project_root_directory
├── .gitignore
├── .github
├── pyproject.toml 
├── README.rst
├── LICENSE
└── mypkg
    ├── __init__.py
    └── file.py

Packaging essentials from my pyproject.toml file:

[build-system]
requires = [
    "setuptools >= 65.5.1",
    "setuptools-scm",
    "wheel"
]
build-backend = "setuptools.build_meta"

[tool.setuptools.package-data]
"*" = [
    "README.rst",
    "LICENSE"
]

[tool.setuptools.packages.find]
include = [
    "mypkg.*"
]

The problem is that both .gitignore and .github are part of SOURCES.txt and therefore are in dist files (e.g. when I unpack mypkg-0.0.0.tar.gz it contains .github directory and .gitignore files).

SOURCES.txt entries:

.gitignore
LICENSE
README.rst
pyproject.toml
.github/dependabot.yml
.github/pull_request_template.md
.github/ISSUE_TEMPLATE/99_any.md
.github/workflows/testing.yml
mypkg.egg-info/PKG-INFO
mypkg.egg-info/SOURCES.txt
mypkg.egg-info/dependency_links.txt
mypkg.egg-info/requires.txt
mypkg.egg-info/top_level.txt
mypkg/__init__.py
mypkg/file.py

I want my SOURCES.txt to contain following entries after build command:

LICENSE
README.rst
mypkg.egg-info/PKG-INFO
mypkg.egg-info/SOURCES.txt
mypkg.egg-info/dependency_links.txt
mypkg.egg-info/requires.txt
mypkg.egg-info/top_level.txt
mypkg/__init__.py
mypkg/file.py

and have my dist files to be build accordingly.


Solution

  • If you must use setuptools-scm, then you have to use MANIFEST.in to control which files you do not want included.

    As standard, setuptools-scm will include every file that is under version control. If this is not the behaviour you desire, then your only option is to use exclude and prune directives in a MANIFEST.in file. A minimal example would be:

    exclude .gitignore
    prune .github
    

    The MANIFEST.in must be located in your repository root. The arguments to exclude and prune are globs. That is, they may be exact filenames (as in the example above), or may include * patterns. For example exclude foo/*.py[co] will exclude all .pyc and .pyo files in the foo directory (relative to the repo root). Full details on all the directives and their options can be found here:

    As mentioned in a comment, it is a good idea to include pyproject.toml. This file provides package managers with the instructions on how to assemble your source files into a package. So without pyproject.toml, your source distributions will not be installable.