Neurobro
Neurobro

Reputation: 262

Multiple subpackages within one package

I am trying to write a package with the following structure

/package
    setup.py

    /subpackage1
        subpackage1.py
        __init__.py
    /subpackage2
        subpackage2.py
        __init__.py
        /utils
            some_other_files_and_codes
            __init__.py

My setup.py currently looks like this:

from setuptools import setup, find_packages

setup(
        name = 'subpackage1',
        version = '1.0',
        install_requires=['numpy', 
                          'scipy'],
        packages = find_packages(),                       
      )

I then install it using pip install -e . from the /package folder. However, I am not able to import subpackage2, only subpackage1.

I would like to be able to import them as

from package import subpackage1
from package import subpackage2

This is important because subpackage1 and subpackage2 exist as standalone packages too on my system.

Could someone help me with this?

Upvotes: 3

Views: 1808

Answers (1)

sinoroc
sinoroc

Reputation: 22275

The snippets you are showing do not make sense. Looks like there's a misunderstanding, in particular there's probably confusion between the name of the Python project and the names of the top-level importable packages.

In the setuptools.setup() function call, the parameter to the name argument should be the name of the project, not the name of an importable top level package. They can be the same names, but not necessarily.

The following might make it more explicit:

MyPythonProject
├── my_importable_package_one
│   ├── __init__.py
│   └── my_module_foo.py
├── my_importable_package_two
│   ├── __init__.py
│   └── my_module_bar.py
└── setup.py

setup.py

import setuptools
setuptools.setup(
    name='MyPythonProject',
    version='1.2.3',
    packages=['my_importable_package_one', 'my_importable_package_two'],
    # ...
)
from my_importable_package_one import my_module_foo
from my_importable_package_two import my_module_bar

Maybe this article on the terminology of Python packaging might help.

Upvotes: 2

Related Questions