'python setuptools setup.cfg: Including main- and subpackages in the build dir

I would like to create a pip package for my python software using a setup.cfg file. My program is currently structured as following:

mypkg
├── setup.cfg
└── src
    ├── subpkg
    │   └── __init__.py
    ├── __init__.py
    └── file_1.py
    ...

so far, using the setuptools find_packages() function allowed me to add my main package as well as the subpackage to my build package. When using a setup.cfg file however, I am not able to include both in my package anymore. The documentation shows that subpackages can be included by adding the option

[options]
packages = find:
package_dir =
    =src

[options.packages.find]
where = src
include = *

But this only includes the subpackage files and not the main package files in my case. Is there a way for me to include all my files while still making use of a setup.cfg setup file?



Solution 1:[1]

Using the directory layout in your example, I found a couple configurations that worked. Both of these allowed me to import all modules from the src package and src.subpkg.

[options]
packages=find:

and the other doesn't use the find: feature at all.

[options]
packages =
    src
    src.subpkg

According to the setuptools documentation https://setuptools.pypa.io/en/latest/userguide/package_discovery.html

If your packages are not in the root of the repository you also need to configure package_dir:

And since the src package is located in the root of the repository specifying a package_dir is not necessary.

This actually works similarly to find_packages function inside a setup.py file. When called with no arguments, find_packages() in a project, it finds all of your top-level and sub-packages automatically.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1