How to import package sub-modules in Python and avoid accidentally duplicating the module?

别等时光非礼了梦想. 提交于 2019-12-24 21:53:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!