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

前端 未结 5 649
醉梦人生
醉梦人生 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:11

    I believe #2 is impossible without using relative imports or the named package. You have to specify what module to import either by explicitly calling its name or using a relative import. otherwise how would the interpreter know what you want?

    If you make your application launcher one level above top_level/ and have it import top_level you can then reference top_level.* from anywhere inside the top_level package.

    (I can show you an example from software I'm working on: http://github.com/toddself/beerlog/)

提交回复
热议问题