Tag Archives: flask init-db Error

flask init-db Error: Error: Could not locate a Flask application. Use the ‘flask –app’ option, ‘FLASK_APP’ environment variable, or a ‘wsgi.py’ or ‘app.py’ file in the current directory.

1. Problem description

When flask initializes the database file, execute flask init-db to report Error: Could not locate a Flask application. Use the ‘flask –app’ option, ‘FLASK_APP’ environment variable, or a ‘wsgi.py’ or ‘app.py’ file in the current directory, the specific error screenshot is as follows:

 

2. Problem solving

This problem is really annoying. I can’t find the corresponding solution on Baidu. The official document about the flask initialization database is also very vague. I wrote an execution command directly, so I don’t understand it very well, so I do it when initializing the database. The operation is:

1. Shut down the previously started server, open a new terminal and execute the following command:

1
2
3
%export FLASK_APP=flaskr
export FLASK_ENV=development
%flask run

 2. Open another terminal and cd to the package directory to execute the flask inti-db command, and the above error is reported. Obviously this is the wrong step.

3. Solutions

After thinking about this problem for a long time, I want to understand where I am wrong. The correct execution steps should be:

1. Execute the following command

1
2
3
%export FLASK_APP=flaskr
export FLASK_ENV=development
%flask init-db

 2. After the execution is successful, a flaskr.sqlite file will be created in the instance folder. The actual result is shown as follows: