1st approach:
I got the following structure:
- project_root - also contains some configs, a .gitignore file, etc.
- start.py
- flask_root
- __ __ INIT. RU
- application.py
- module_1
- module_2
The topmost start.py launches the application:
#! /usr/bin/env python from flask_root import applicaiton if __name__ == '__main__': application.manager.run()
Python searches for packages in the directory from which you are the script, so now you do not need to add them to sys.path (as for me, changing sys.path looks ugly). Now you have the full flask_root python working package, and you can import all of it from anywhere in your application:
from flask_root.application import db
Second approach:
If you run the Flask application from it,
./application.py runserver
the directory you started from cannot be accessed as a python package, even if it has __init__.py in it.
Although in your catalog layout you can do the following trick:
models / __ __ INIT ru :.
from application import db ...
models /user.py
from . import db ...
The first approach is cleaner and more universal. The second may be useful when you need to share the same drawings between multiple Flask projects.
source share