How to reference to the top-level module in Python inside a package?

前端 未结 5 565
醉梦人生
醉梦人生 2021-02-01 17:51

In the below hierachy, is there a convenient and universal way to reference to the top_package using a generic term in all .py file below? I would like to have a consistent way

5条回答
  •  遇见更好的自我
    2021-02-01 18:22

    This works from within a library module:

    import __main__ as main_package
    TOP_PACKAGE = main_package.__package__.split('.')[0]
    

提交回复
热议问题