Is there a way to run flask in debug mode using the `flask run` command without setting environment variables?
In short, there does not seem to be a way of using flask run
how I want without assigning environment variables, however using a .flaskenv
file will allow environment variables to be loaded at run time.
The .flaskenv
file for example could include the following ENVs among others to be loaded:
FLASK_APP=main:app
FLASK_ENV=development
FLASK_DEBUG=1
Note - this does require python-dotenv
to be installed to use.
All credit to @cizario who answered here with some more detail:
https://stackoverflow.com/a/64623193/12368419
How can I debug a Flask App using gunicorn
Try this below code
def create_app(config_class=Config):
app = Flask(__name__)
app.config['DEBUG'] = True
...
Even if you set app.debug = True, you will still only get an empty page with the message Internal Server Error if you run with gunicorn testserver:app. The best you can do with gunicorn is to run it with gunicorn --debug testserver:app. That gives you the trace in addition to the Internal Server Error message. However, this is just the same text trace that you see in the terminal and not the Flask debugger.
Adding the if name ... section to the testserver.py and running python testserver.py to start the server in development gets you the Flask debugger.
So, will suggest you do not use gunicorn in development environment, it will not help you in the proper way.
Gunicorn is a pre-forking environment and apparently the Flask debugger doesn't work in a forking environment.
How do I debug Flask App in VS Code
As of November 2019 I the found the following useful:
New Way (basic & flakey) - "Old Way" Below is Better
- https://code.visualstudio.com/docs/python/tutorial-flask
Assuming a simple app.py such as:
import flask
app = flask.Flask(__name__)
@app.route('/')
def index():
return "Hello world!"
And .vscode/launch.json added to your project by adding Python Flask Debug Configuration from the Debug Explorer drop down.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py",
"FLASK_ENV": "development",
"FLASK_DEBUG": "1"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
}
]
}
The Flask app is effectively run the "new way" from the VS Code debugger [F5].
python -m flask run
Old Way (better)
Miguel suggests running apps the old way, with additional flags, is better in the VS Code debugger.
- https://youtu.be/UXqiVe6h3lA?t=1194
- per https://blog.miguelgrinberg.com/post/setting-up-a-flask-application-in-visual-studio-code.
Add the following to app.py (from above):
if __name__ == '__main__':
app.run(use_debugger=False, use_reloader=False, passthrough_errors=True)
Modify .vscode/launch.json to look as follows:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "app",
"env": {
"FLASK_APP": "app.py",
"FLASK_ENV": "development",
"FLASK_DEBUG": "1"
},
"args": [
// "run",
// "--no-debugger",
// "--no-reload"
],
"jinja": true
}
]
}
So the Flask app is effectively run the "old way" from the VS Code debugger [F5].
python app.py
Why can't I debug my flask app with VSCode
To debug flask with VSCode you need to set your setting.json
and launch.json
files correctly, here is a possible configuration:
settings.json
{
"python.pythonPath": "<your_python_venv_path>",
}
launch.json
{
"version": "0.2.0",
"configurations" : [
{
"name": "local",
"type": "python",
"stopOnEntry": false,
"request": "launch",
"program" : "${workspaceFolder}/<path_to_run.py>",
"console" : "integratedTerminal",
"justMyCode" : false,
"cwd": "${workspaceFolder}"
}
]
}
Also take a look here
VS Code: Unable to debug Connexion/Flask app?
Just in case anybody else has the same problem in the future.
This seems to be some kind of VS Code bug.
I tried starting my app via the command line and it did work. After that, I reinstalled all python packages in my VS Code venv (via my requirements.txt) but it didn't help.
Ultimately removing the virtual environment I used in VS code and making a new one solved my problem.
Related Topics
Installing Python Module Within Code
Remove All the Elements That Occur in One List from Another
How to Add a New Column to an Existing Dataframe
How to Call a Function Within a Class
Split String With Multiple Delimiters in Python
Are For-Loops in Pandas Really Bad? When Should I Care
Understanding Inplace=True in Pandas
Python: Justifying Numpy Array
Can a Variable Number of Arguments Be Passed to a Function
Prevent Scientific Notation in Matplotlib.Pyplot
How to Uninstall Python 2.7 on a MAC Os X 10.6.4
How to Properly Determine the Current Script Directory
Converting Unix Timestamp String to Readable Date
How to Convert All Strings in a List of Lists to Integers
Understanding Repr( ) Function in Python
Why Should We Not Use Sys.Setdefaultencoding("Utf-8") in a Py Script