Matthias
Matthias

Reputation: 10399

How to get Flask application working in VS Code?

I have copied the example from the Flask-RESTful quick start page into a file run.py and started it from the command line, which works fine:

mfb@Areion:/data/Development/Python/MLserver$ python server/run.py 
 * Serving Flask app "run" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 167-198-829

However, when I start this from VS Code I get the following output & error message:

mfb@Areion:/data/Development/Python/MLserver$  cd /data/Development/Python/MLserver ; env /usr/local/bin/python /home/mfb/.vscode/extensions/ms-python.python-2020.6.89148/pythonFiles/lib/python/debugpy/launcher 34441 -- /data/Development/Python/MLserver/server/run.py 
 * Serving Flask app "run" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
No module named run

Why does it not find that module named "run"?
Do I need something in my launch configuration? Currently it looks like this:

"configurations": [
    {
        "name": "Start MLserver",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/server/run.py",
        "env": {
            "FLASK_ENV": "development",
        },
        "console": "integratedTerminal"
    }

Btw. it does work when starting via Ctrl+F5, so the bug is related to the VS Code debugger somehow.

Upvotes: 0

Views: 3786

Answers (1)

Mohamed Farid
Mohamed Farid

Reputation: 131

python need to know where is Flask App

edit configurations and add FLASK_APP variable to env

*edit replace configurations with

"configurations": [
  {
    "name": "Python: Flask",
    "type": "python",
    "request": "launch",
    "module": "flask",
    "env": {
      "FLASK_APP": "server/run.py",
      "FLASK_ENV": "development",
      "FLASK_DEBUG": "0"
    },
    "args": ["run", "--no-debugger", "--no-reload"],
    "jinja": true
  }
]

watch this will help in this topic

Setting Up a Flask Application in Visual Studio Code

Upvotes: 2

Related Questions