Get root path of Flask application
<p>I'm working on a Flask extension from which I want to create a directory in the project's root path on the file system.</p>

<p>Suppose we have this directory structure</p>

<pre><code>/project
    /app
    /tests
    /my_folder
    manage.py
</code></pre>

<p>my_folder should be created dynamically by the extension, which is a test utility and wraps the application under test in the /tests directory. However, I'm struggling to determine the project's root path within my extension.</p>

<p>For now, I am trying to guess the path from the run file:</p>

<pre><code>def root_path(self):
    # Infer the root path from the run file in the project root (e.g. manage.py)
    fn = getattr(sys.modules['__main__'], '__file__')
    root_path = os.path.abspath(os.path.dirname(fn))
    return root_path
</code></pre>

<p>This obviously breaks as soon as the tests are run from within the IDE instead of the manage.py. I could simply infer the project's root relative to the app or tests directory, but I don't want to make any assumptions regarding the name or structure of these directories (since multiple apps might be hosted as subpackages in a single package).</p>

<p>I was wondering if there is a best practice for this type of problem or an undocumented method which the Flask object provides (such as get_root_path). </p>