27

I use setuptools. In setup.cfg, I can define

[options.package_data]
myModule =
    '*.csv'

to make sure that data will be installed for my users.

Can I achive the same with pyproject.toml instead?

LudvigH
  • 3,662
  • 5
  • 31
  • 49

6 Answers6

25

Starting in setuptools version 61.0.0 there is beta support for this feature. See the documentation https://setuptools.pypa.io/en/stable/userguide/datafiles.html

The syntax is

[tool.setuptools.package-data]
myModule = ["*.csv"]
LudvigH
  • 3,662
  • 5
  • 31
  • 49
  • 1
    If you have *both* `setup.cfg` and `pyproject.toml`, adding the above in `pyproject.toml` seems not to work. – bitinerant Dec 06 '22 at 19:44
  • You may need: `find . -name '*.egg-info' -exec rm -r {} + && rm -Rf build` – bitinerant Dec 06 '22 at 19:45
  • 3
    Note: if your project is namespaced, the syntax is `"path.to.myModule" = [...]` i.e. you need quotes around the key – Alice Purcell Dec 21 '22 at 10:25
  • Although this answer is sufficient for the question posed here, it's not the best on the page. See https://stackoverflow.com/a/76435649/567059 for a more complete example, and one that matches the recommended package structure to use when creating packages. – David Gard Aug 03 '23 at 09:01
5

if I understand your concern, you are using setuptools as a building and distributing system and you want to move some configs from setup.[py,cfg] namely package_data to pyproject.toml, if so you have to use an other tool to build and distribute your package e.g poetry as stated in @Romibuzi's answer because it's not possible unless the setuptools' team plan a new major release to include a full support of pyproject.toml and then no need for extra/standalone config setup.cfg file.

some references:

Update

as per v61.0.0, setuptools brings a partial support (still in beta version) for this feature.

refer to @LudvigH's answer. (thank you btw)

cizario
  • 3,995
  • 3
  • 13
  • 27
1

In addition to the other existing answers, one can also simply use a MANIFEST.in file.

See https://packaging.python.org/en/latest/guides/using-manifest-in/

Michele Peresano
  • 154
  • 1
  • 11
  • This is a good complementary point. It often serves a different purpose than `package_data`, but can still be very useful. – LudvigH Apr 28 '23 at 09:49
1

As of now, if you are using pyproject.toml and setuptool you do not need to do anything. Just add some extra files in the same directory as your Python code.

But note that it will only add them if they are checked into source control, and you are using setuptools-scm (the example tells you to so you probably are).

For example this is my pyproject.toml

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

[project]
name = "foo"
authors = [
    {name = "...", email = "..."},
]
description = "..."
readme = "README.md"
requires-python = ">=3.9"
dependencies = [
    "pydantic",
    "jinja2"
]
version = "1.0.1"

[project.scripts]
foo = "foo:main"

And I have these files:

.
├── pyproject.toml
├── pyrightconfig.json
├── README.md
├── setup.py
└── src
    └── foo
        ├── command.py
        ├── index.html
        ├── __init__.py
        ├── render.py
        └── triage.py

And when I run python3 -m build it does add index.html to the wheels.

Also setup.py is just for backwards compatibility. You don't actually need it.

Timmmm
  • 88,195
  • 71
  • 364
  • 509
0

If you're using Hatchling, per the docs, you can also do this via:

[tool.hatch.build]
include = [
  "myModule/**/*.csv",
  "**/*.py",  
]
exclude = [
  "tests/**",
]
brandonscript
  • 68,675
  • 32
  • 163
  • 220
0

Assuming a project dir structure like

project_root_directory
├── pyproject.toml 
└── src
    └── mypkg
        ├── __init__.py
        ├── data1.rst
        ├── data2.rst
        ├── data1.txt
        └── data2.txt
 

simply add the following to your pyproject.toml

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
mypkg = ["*.txt", "*.rst"]

More info can be found in the docs @ https://setuptools.pypa.io/en/latest/userguide/datafiles.html

Larry E
  • 19
  • 3
  • Indeed. That is also what my answer indicate, and you provide the same link as I do. – LudvigH Jun 09 '23 at 09:16
  • Ah not sure how i missed that! wanted to add a quick example anyway for anyone searching for a quick solution :) – Larry E Jun 09 '23 at 15:17
  • This answer is better than https://stackoverflow.com/a/73062301/567059 because it showed the need for `[tool.setuptools.packages.find]`. – David Gard Aug 03 '23 at 09:02