问题
I have a package with submodules, and I want to ensure I don't duplicate the module (e.g. import it with different absolute paths and accidentally have the module symbols be duplicated).
For example:
my_package/
__init__.py
my_submodule.py
I want to be able to import my_package.my_submodule
globally and something like import my_submodule
from within the package. How do I do this?
EDIT: This is effectively a duplicate of this thread: Relative imports for the billionth time
However, I've denoted one of the primary failure modes of this in the title (duplication / double-import).
来源:https://stackoverflow.com/questions/57858801/how-to-import-package-sub-modules-in-python-and-avoid-accidentally-duplicating-t