问题
Despite trial and error I cannot get the command 'flask shell' to run.
This is what happens when i run flask shell from the project dir:
$ set FLASK_APP=
Error: While importing "project_dir.wsgi", an ImportError was raised: [...] from flask_app.app import construct_app ModuleNotFoundError: No module named 'flask_app'
$ set FLASK_APP=wsgi
ModuleNotFoundError: No module named 'flask_app'
$ set FLASK_APP=flask_app\__init__.py:construct_app
Error: module 'backoffice_nova.flask_app' has no attribute 'construct_app'
$ set FLASK_APP=flask_app:contruct_app("development")
Error: module 'backoffice_nova.flask_app' has no attribute 'contruct_app'
My project structure looks like this
/project_dir
.
+-- flask_app
¦ +-- __init__.py
¦ +-- app
¦ +-- main
¦ ¦ +-- __init__.py
¦ +-- static
¦ +-- templates
¦ +--models.py
¦ +-- __init__.py
+-- src
¦ +-- __init__.py
¦ +-- ...
+-- tests
¦ +-- __init__.py
¦ +-- ...
+-- environment.yml
+-- wsgi.py
+-- config.py
To start the app I normally run wsgi.py
#wsgi.py
from flask_app.app import construct_app
app = construct_app("development")
if __name__ == "__main__":
app.run(host='localhost', debug=True)
回答1:
Flask will automatically find the application instance if you put the app in wsgi.py or app.py and name the instance as app or application, so you don't need to set the FLASK_APP.
Besides, you can explicitly set it as:
$ set FLASK_APP=wsgi
or:
$ set FLASK_APP=flask_app:contruct_app("development")
See more about application discovery in the docs.
回答2:
i would recommend you this good read on How to Run a Flask Application from Miguel GRINBERG.
instead of manually setting FLASK_APP on windows cmd or Power Shell just create .flaskenv file under the root of your project and add the Flask related environment variables (don't forget to install python-dotenv package)
i usually import my Flask app as a package not module and as a good practice __init__.py is kept for imports and some metadata like (__author__ and __version__)
and with a good project structure, you can make your app maintainable, easy to debug, extensible and easy to deploy
i usually set the structure of my flask project to some thing similar to below.
have a look at .flaskenv, __init__.py, flaskdemo.py and wsgi.py files
project structure
flaskdemo / the root folder project
..
.. congfigs
__init__.py
base.py
development.py
production.py
testing.py
.. flaskdemo
..
.. errors (blueprint)
templates
__init__.py
views.py
.. main (blueprint)
.. static
.. templates
.. __init__.py
.. forms.py
.. models.py
.. views.py
.. utils.py
.. static
.. templates
.. __init__.py
.. extensions.py
.. flaskdemo.py
.. tests
.. venv
.. .flaskenv
.. wsgi.py
.. environment.yml
..
/.flaskenv
FLASK_APP=flaskdemo:create_app('development')
# FLASK_APP=flaskdemo:create_app('testing')
# FLASK_APP=flaskdemo:create_app('production')
FLASK_ENV=development
FLASK_DEBUG=0
/flaskdemo/__init __.py
from .flaskdemo import create_app
/flaskdemo/flaskdemo.py
from flask import Flask
[..]
def create_app(config_object):
"""Create a Flask application using the app factory pattern."""
app = Flask(__name__)
load_config(app, config_object)
init_app_extensions(app)
register_blueprints(app)
[..]
return app
/flaskdemo/wsgi.py
below how i do to serve flask app on windows using apache and mod_wsgi
import os
import sys
# activate virtualenv
PROJECT = "flaskdemo"
# py -m venv
# @see: https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
# @see: https://stackoverflow.com/questions/25020451/no-activate-this-py-file-in-venv-pyvenv
activate_this = os.path.join(os.environ['PYTHON_HOME'], 'myapps/flask', PROJECT, 'venv/Scripts/activate_this.py')
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
BASE_DIR = os.path.join(os.path.dirname(__file__))
if BASE_DIR not in sys.path:
sys.path.append(BASE_DIR)
from helloflask import create_app
application = create_app('development')
# application = create_app('testing')
# application = create_app('production')
来源:https://stackoverflow.com/questions/62712066/how-to-set-flask-app-with-custom-project-structure