Python package not installing submodules

Question:

I have created a package with the following structure in the dev branch (not merging to main until I verify the package installs correctly):

mypackage
|
|-- __init__.py
|-- setup.py
|-- requirements.txt
|-- module.py
|-- subpackage_one
    |
    |-- __init__.py
    |-- module_ab.py
        |-- class_aba
        |-- class_abb
    |-- module_ac.py
        |-- function_aca

|-- subpackage_two
    |
    |-- __init__.py
    |-- module_ba.py
        |-- function_baa

Additional information:

  • The __init__.py files at root and in subpackage__two are both empty
  • The __init__.py file in subpackage_one contains some additional initialization in the form of from mypackage.subpackage_one.module_xx import class_xxx (or function_xxx)
  • I am installing the package via pip install git+https://github.com/organization/repo.git@dev
  • If I am in the root directory of the package, I can import the submodules as expected
  • The setup.py file is:
import setuptools

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

setuptools.setup(
    name='mypackage',
    version='0.0.2',
    author='author1, author2',
    author_email='author1_email, author2_email',
    description='My Package',
    long_description=long_description,
    long_description_content_type="text/markdown",
    url='https://github.com/organization/repo',
    packages=['mypackage'],
    install_requires=['requests'],
)
  • When I run the following snippet:
import pkgutil
for i in pkgutil.iter_modules(mypackage.__path__):
    print(i)

I see:

ModuleInfo(module_finder=FileFinder('/path/to/package/mypackage'), name='module', ispkg=False)

And indeed, the subpackages are not in the mypackage folder.

How can I get the subpackages to install along with the package?

Asked By: acciolurker

||

Answers:

Your issue might be the packages parameter. It needs to be supplied with every module or ‘package’.

setuptools has a nice function to find them, use it like this: packages=setuptools.find_namespace_packages(),

Answered By: Mandera
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.