0

I'm using Python 3.8.2, and I've created a namespaced package, called fruit. The structure of this namespaced package is as follows:

fruit
└── banana
    └── __init__.py

The __init__.py file here only contains fruit="banana".

Note, I'm creating this package using implicit namespace packages (as per Python 3.3), and referring to the packages using packages=find_namespace_packages(include=['fruit.banana', 'fruit.banana.*']) in setup.py.

The issue I'm facing, occurs when using the dependency in a project that uses the same package naming. An example project structure of a project using the fruit dependency:

fruit --> top level package name
├── __init__.py
└── banana
    ├── __init__.py
    └── run.py

Both __init__.py files are empty, the contents of the run.py file are this:

from fruit.banana import fruit

if __name__ == '__main__':
    print(fruit)

When running this run.py, I'm getting a ImportError: cannot import name 'fruit' from 'fruit.banana' (/path/to/project/.../fruit/banana/__init__.py). However, when I change the package name marked with top level package name in the directory structure above to anything else than fruit, it runs fine.

How can I reuse the same package names when creating a namespaced package?

Robin Trietsch
  • 1,662
  • 2
  • 19
  • 31
  • Related to this post: https://stackoverflow.com/q/57857591 – Robin Trietsch Sep 11 '20 at 07:11
  • It seems that the "old" `pkgutil` style namespacing does work, but the new implicit namespacing doesn't. So when declaring something a namespace in both the dependency and the project using the dependency, it does work. – Robin Trietsch Sep 11 '20 at 07:25

0 Answers0