Problem importing nested package in Python

Question:

Based from a project with the following structure:

.
└── src/
    ├── main.py
    ├── PackageA/
    │   ├── __init__.py
    │   ├── logic.py
    │   ├── SubPackageA1/
    │   │   ├── __init__.py
    │   │   └── util.py
    │   └── SubPackageA2/
    │       ├── __init__.py
    │       └── otherUtil.py
    └── PackageB/
        ├── __init__.py
        └── helpers.py

Project structure

It would be possible to import in the file helpers.py the package otherutil.py?

All the combinations I tried until now fail.

Asked By: Archiduque

||

Answers:

If your program is executed from main.py the import in helpers.py should work like this:

from PackageA.SubPackageA2 import otherUtil

Yes, I checked it, main.py:

from PackageB import helpers

print(helpers.HELPERS_UTIL)

otherUtil.py:

OTHER_UTIL = 'test'

helpers.py

from PackageA.SubPackageA2 import otherUtil

HELPERS_UTIL = otherUtil.OTHER_UTIL
Answered By: phibel
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.