partypeter
partypeter

Reputation: 112

Setuptools creates multiple packages instead of one overarching package

I am trying to create a minimalistic example to understand how setuptools works.

I have the following folder structure:

my_package/
  src/
    pkg1/
      __init__.py
    pkg2/
      __init__.py
    excluded_pkg/
      __init__.py
    __init__.py
setup.py

My setup.py includes this:

from setuptools import find_packages, setup


setup(
    name='inversiontestproblems',
    version='0.0.1',
    install_requires=[
        'requests',
        'importlib-metadata; python_version == "3.8"',
    ],
    packages=find_packages(
        where='src',
        include=['pkg*'],
        exclude=['excluded_pkg'],
    ),
    package_dir={"": "src"},
)

If I create wheel with this setup and install it with pip install, it makes pkg1 and pkg2 directly accessible and drops the package name. What this means is I could open a python terminal and type import pkg1, but I can not write import my_package.

To fix it I tried to change setup.py to this:

...
package_dir={"my_package": "src"},
...

But now I get this error: error: package directory 'pkg1' does not exist. This error was also discussed here, here they suggested to change "my_package" to "" to make it work: Python setuptools: package directory does not exist

I am at my wits end. The Setuptools website does not help either. I would appreciate some help. How to I create a single package containing sub-packages with setuptools?

Upvotes: 1

Views: 1539

Answers (1)

YYLIZH
YYLIZH

Reputation: 36

please try to rearrange your directory structure like below

my_package
   ├── my_package
   │   ├── excluded_pkg
   │   │   └── __init__.py
   │   ├── __init__.py
   │   ├── pkg1
   │   │   └── __init__.py
   │   └── pkg2
   │       └── __init__.py
   └── setup.py

In setup.py, the code will be

from setuptools import find_packages, setup


setup(
    name='inversiontestproblems',
    version='0.0.1',
    packages=find_packages(
        exclude=['my_package.excluded_pkg'],
    ),

)

Hope this solve your problem.

Upvotes: 1

Related Questions