[BUG]
setuptools version
setuptools==65.5.0
Python version
Python 3.10.10
OS
Windows
Additional environment information
No response
Description
Files excluded via the find_packages(exclude= mechanism appear in the package when built with build package
Expected behavior
Excluded files should be excluded as they are when building directly with setup.py
How to Reproduce
- create project with
libandtestsdirectories libshould contain an empty__init__.pyfile and ahello.pyfile withprint("hello")testsshould contain an emptytest_hello.pyfilesetup.pyshould contain the following
from setuptools import find_packages, setup
setup(
name="hello",
version="0.1",
author="myself",
description="test package",
packages=find_packages(exclude=["tests"]),
)
### Output
When built with `python setup.py sdist` the contents of `hello.egg-info/SOURCERS>.txt` does **not** contain `tests/test_hello.py` (this is the expected behaviour).
When built with `python -m build` the contents of `hello.egg-info/SOURCERS>.txt` **does** contain `tests/test_hello.py` (expected to be excluded)
Note, I've opened this defect after starting discussion https://discuss.python.org/t/moving-to-build-rather-than-calling-setup-py-directly/25107 and being told this looks like a defect.
Hi @lanzkron, thank you very much for opening the issue.
Have you tried adding a pyproject.toml file?
I tried the following and it seems to result in the expected outcome:
> docker run --rm -it python:3.10-windowsservercore powershell
cd $env:TEMP
mkdir myproj
cd myproj
mkdir lib
new-item lib/__init__.py
echo 'print("hello")' > lib/hello.py
mkdir tests
new-item tests/test_hello.py
new-item setup.py
@"
from setuptools import find_packages, setup
setup(
name="hello",
version="0.1",
author="myself",
description="test package",
packages=find_packages(exclude=["tests"]),
)
"@ | add-content -Path setup.py
new-item pyproject.toml
@"
[build-system]
build-backend = "setuptools.build_meta"
requires = ["setuptools==65.5"]
"@ | add-content -Path pyproject.toml
python -m pip install build
python -m build
tar xOf .\dist\hello-0.1.tar.gz hello-0.1/hello.egg-info/SOURCES.txt
# pyproject.toml
# setup.py
# hello.egg-info/PKG-INFO
# hello.egg-info/SOURCES.txt
# hello.egg-info/dependency_links.txt
# hello.egg-info/top_level.txt
# lib/__init__.py
# lib/hello.py
Other than that, I would also recommend removing the caching folders/files (e.g. hello.egg-info, build, dist)
Remark: To control the contents of sdist, it is preferable to use a plugin (e.g. setuptools-scm) or MANIFEST.in. You can find some documentation at https://setuptools.pypa.io/en/latest/userguide/miscellaneous.html.
Out of curiosity, I also tried to run with the __legacy__ backend which is supposed to be the default, when you don't have a pyproject.toml file, and it also seems to result in the same outcome:
> docker run --rm -it python:3.10-windowsservercore powershell
cd $env:TEMP
mkdir myproj
cd myproj
mkdir lib
new-item lib/__init__.py
echo 'print("hello")' > lib/hello.py
mkdir tests
new-item tests/test_hello.py
new-item setup.py
@"
from setuptools import find_packages, setup
setup(
name="hello",
version="0.1",
author="myself",
description="test package",
packages=find_packages(exclude=["tests"]),
)
"@ | add-content -Path setup.py
new-item pyproject.toml
@"
[build-system]
build-backend = "setuptools.build_meta:__legacy__"
requires = ["setuptools==65.5"]
"@ | add-content -Path pyproject.toml
python -m pip install build
python -m build
tar xOf .\dist\hello-0.1.tar.gz hello-0.1/hello.egg-info/SOURCES.txt
# setup.py
# hello.egg-info/PKG-INFO
# hello.egg-info/SOURCES.txt
# hello.egg-info/dependency_links.txt
# hello.egg-info/top_level.txt
# lib/__init__.py
# lib/hello.py
Since running the following also work as expected (no isolation), I can only assume that when pyproject.toml is not specified, somehow an old version of setuptools ends up being used:
cd $env:TEMP
mkdir myproj
cd myproj
mkdir lib
new-item lib/__init__.py
echo 'print("hello")' > lib/hello.py
mkdir tests
new-item tests/test_hello.py
new-item setup.py
@"
from setuptools import find_packages, setup
setup(
name="hello",
version="0.1",
author="myself",
description="test package",
packages=find_packages(exclude=["tests"]),
)
"@ | add-content -Path setup.py
python -m pip install build 'setuptools==65.5'
python -m build -n
tar xOf .\dist\hello-0.1.tar.gz hello-0.1/hello.egg-info/SOURCES.txt
# setup.py
# hello.egg-info/PKG-INFO
# hello.egg-info/SOURCES.txt
# hello.egg-info/dependency_links.txt
# hello.egg-info/top_level.txt
# lib/__init__.py
# lib/hello.py
Cross posting for the sake of completeness:
In general, if you use the same version of setuptools, python setup.py sdist should be pretty similar to python -m build --sdist (except when something gets cached accidentally).
But please note that find_packages(exclude=...) is not a direct indicative of what goes in the sdist. This configuration will rather interfere what goes on the wheel. If you are not using a plugin like setuptools-scm (or configuring a MANIFEST.in) , setuptools will fallback to a default set of files that might automatically include docs, tests, etc…
I will leave this issue open for a while to collect feedback, but my intention is to close it soon since I don't see any action points for setuptools.
Adding pyproject.toml as suggested solved the issue. Thanks!