Python - Get path of root project structure

后端 未结 16 1827
陌清茗
陌清茗 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:40

    Just an example: I want to run runio.py from within helper1.py

    Project tree example:

    myproject_root
    - modules_dir/helpers_dir/helper1.py
    - tools_dir/runio.py
    

    Get project root:

    import os
    rootdir = os.path.dirname(os.path.realpath(__file__)).rsplit(os.sep, 2)[0]
    

    Build path to script:

    runme = os.path.join(rootdir, "tools_dir", "runio.py")
    execfile(runme)
    

提交回复
热议问题