Python - Get path of root project structure

后端 未结 16 1820
陌清茗
陌清茗 2020-12-04 08:03

I\'ve got a python project with a configuration file in the project root. The configuration file needs to be accessed in a few different files throughout the project.

16条回答
  •  天命终不由人
    2020-12-04 08:41

    To get the path of the "root" module, you can use:

    import os
    import sys
    os.path.dirname(sys.modules['__main__'].__file__)
    

    But more interestingly if you have an config "object" in your top-most module you could -read- from it like so:

    app = sys.modules['__main__']
    stuff = app.config.somefunc()
    

提交回复
热议问题