Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get root path of Flask application

Tags:

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.

Suppose we have this directory structure

/project     /app     /tests     /my_folder     manage.py 

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.

For now, I am trying to guess the path from the run file:

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 

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).

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).

like image 797
thyrel Avatar asked Apr 15 '16 14:04

thyrel


People also ask

How do I get the root path in Python?

The configuration file needs to be accessed in a few different files throughout the project. So it looks something like: <ROOT>/configuration. conf <ROOT>/A/a.py , <ROOT>/A/B/b.py (when b,a.py access the configuration file).

How do I find the path of a Python project?

First, you have to import the os module in Python so you can run operating system functionalities in your code. Then you create the variable absolute_path which fetches the current directory relative to the root folder. This is the full path to your working directory, in this case, ~/home/projects/example-project/ .

What is app route in flask?

App Routing means mapping the URLs to a specific function that will handle the logic for that URL. Modern web frameworks use more meaningful URLs to help users remember the URLs and make navigation simpler. Example: In our application, the URL (“/”) is associated with the root URL.


2 Answers

app.root_path contains the root path for the application. This is determined based on the name passed to Flask. Typically, you should use the instance path (app.instance_path) not the root path, as the instance path will not be within the package code.

filename = os.path.join(app.instance_path, 'my_folder', 'my_file.txt') 
like image 179
davidism Avatar answered Oct 05 '22 13:10

davidism


app.root_path is the absolute path to the root directory containing your app code.

app.instance_path is the absolute path to the instance folder. os.path.dirname(app.instance_path) is the directory above the instance folder. During development, this is next to or the same as the root path, depending on your project layout.

like image 27
Chanpreet Chhabra Avatar answered Oct 05 '22 13:10

Chanpreet Chhabra