Carsten
Carsten

Reputation: 2040

Python setuptools: package directory does not exist

I have a project with this setup.py file:

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="",
    version="0.0.1",
    author="",
    author_email="",
    description="",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=setuptools.find_packages(where="./src", exclude=("./tests",)),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.8',
)

This is my project directory structure (first two levels):

$ tree -L 2
.
├── README.md
├── setup.py
├── src
│   └── my_pkg
└── tests
    ├── conftest.py
    ├── data
    ├── __init__.py
    ├── integration
    ├── __pycache__
    └── unit

When I run any setuptools command, I get the following error:

$ python setup.py build
running build
running build_py
error: package directory 'my_pkg' does not exist

The same happens for other commands like python setup.py develop and python setup.py bdist-wheel.

I suspect that it has to do with the src directory, as specified in the find_packages(where="./src") call in the setup.py. However, I've been following the documentation, and it does look the the my_pkg module is discovered at some point.

Why does build_py fail to find it?

Upvotes: 15

Views: 11564

Answers (1)

Dunes
Dunes

Reputation: 40658

find_packages() automatically generates package names. That is, in your case all it does is generate ['my_pkg']. It doesn't actually tell setup() where to find my_pkg, just that it should expect to find a package called my_pkg somewhere. You have to separately tell setup() where it should look for packages. Is this confusing and counter intuitive? Yes. Anyway, you can tell setup() where to find my_pkg by using the package_dir argument. eg.

package_dir={"":"src"}

Upvotes: 20

Related Questions