How to add a txt file to my package?
I have mydir/myfile.txt. How to add to my package?
I’m using pyproject.toml and ‘python -m build’. For *.py files I can create the package.
How to add a txt file to my package?
I have mydir/myfile.txt. How to add to my package?
I’m using pyproject.toml and ‘python -m build’. For *.py files I can create the package.
It depends on which backend you’re using. What’s in your pyproject.toml
under [build-system]
.
e.g. for setuptools: 4. Creating a Source Distribution - setuptools 74.0.0.post20240830 documentation
Yes I have setuptools.
If you don’t mind adding everything under your versioning control system (e.g. git
) to the sdist. I would add setuptools-scm
to [build-system] requires
in pyproject.toml
.
If you are exclusively using pyproject.toml
(no setup.py
or setup.cfg
that would do thr trick (include-package-data
is True
by default in pyproject.toml
).
This assuming that this .txt exists inside your package directory.
Thanks but it does not add my text file
Here is my toml file
Im using Python 3.9 (can’t change)
[build-system]
requires = ["setuptools>=61", "setuptools-scm>=8"]
build-backend = "setuptools.build_meta"
[project]
name = "tpack_t"
version = "0.0.1"
authors = [
{ name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
Homepage = "https://github.com/pypa/sampleproject"
Issues = "https://github.com/pypa/sampleproject/issues"
Sorry, I probably assumed that you were already using a version control system (VCS) like git. If you haven’t started using one yet that is probably why you don’t see the .txt
file in your wheel.
This is a full example using git (which is my personal preferred way of dealing with this) for the given pyproject.toml
:
# docker run --rm -it python:3.12-bookworm /bin/bash
mkdir -p /tmp/project
cd /tmp/project
cat <<EOF > pyproject.toml
[build-system]
requires = ["setuptools>=61", "setuptools-scm>=8"]
build-backend = "setuptools.build_meta"
[project]
name = "tpack_t"
version = "0.0.1"
authors = [
{ name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
Homepage = "https://github.com/pypa/sampleproject"
Issues = "https://github.com/pypa/sampleproject/issues"
EOF
mkdir -p src/tpack_t
touch src/tpack_t/test.txt
# Add files to git
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git init .
git add .
git commit -m "My first commit"
python -m pip install -U build
python -m build
unzip -Z1 dist/*.whl
# tpack_t/test.txt <------------------------------------- this one
# tpack_t-0.0.1.dist-info/METADATA
# tpack_t-0.0.1.dist-info/WHEEL
# tpack_t-0.0.1.dist-info/top_level.txt
# tpack_t-0.0.1.dist-info/RECORD
If you don’t use git
(or don’t want to use this method) then you will need to create a MANIFEST.in
file, as documented in Controlling files in the distribution - setuptools 74.0.0.post20240902 documentation (the example of MANIFEST.in
in the docs is a good starting point for an “average” Python project that uses the src
-layout).