Cannot import partially initialized subpackage

63 views Asked by At

So, this is a question about how to use __init__.py with sub-packages. I have searched, and surprisingly not found a decent answer to this.

I have the following structure:

my_package/
setup.py
    my_package/
        __init__.py
        module1.py
            my_sub_package/
            __init__.py
            module2.py

The contents of my_package/__init__.py is

from . import my_sub_package
from .module1 import *

The contents of my_package/my_sub_package/__init__.py is

from .module2 import *

The contents of setup.py is

from setuptools import setup

setup(
    name='My Package',
    packages=['my_package'],
    version='1.0.0'
)

I'm able to import the package in python from parent folder using:

import my_package

The problem arises when I install the package and run this in another folder (not containing my_package). I install the package using:

pip3 install my_package/ --upgrade --force-reinstall

Then, when I try to import it in Python

import my_package

ImportError: cannot import name 'my_sub_package' from partially initialized module 'my_package' (most likely due to a circular import) (<PYTHON_PATH>/lib/python3.11/site-packages/my_package/__init__.py)

What am I doing wrong?

I am doing exactly as they say in this answer: Sub packages and __init__.py

1

There are 1 answers

0
FriendlyBulbasaur On

Apparently, the problem lies in setup.py, where I need to specify that also my_sub_package is a package:

from setuptools import setup
setup(
    name='My Package',
    packages=['my_package','my_package.my_sub_package],
    version='1.0.0'
)

An even better solution is to use find_packages

from setuptools import setup, find_packages
setup(
    name='My Package',
    packages= find_packages(),
    version='1.0.0'
)