1

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?

partypeter
  • 92
  • 1
  • 8
  • 1
    I want to understand the last line correctly before writing an answer or to know if I know the answer or not. So you want to do something like `from my_package import pkg1`, right? Or should you be able to `import my_package`, `import pkg1`? – WiLL_K May 11 '22 at 11:36
  • Thanks for the question, you are correct. I want to be able to do `from my_package import pkg1`. But it seems that I can only have one: Either I can use commands like `where=' '` and `exclude=[]`, or I can specify the package name in the line `package_dir={"my_package": "src"}`. But I can't find a way to do both. – partypeter May 12 '22 at 02:45

1 Answers1

1

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.

YYLIZH
  • 26
  • 3