0

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.

Matthias
  • 9,817
  • 14
  • 66
  • 125
  • "env /usr/local/bin/python" why you don't use your virtualenv for your python? try press F1, write "Python: select interpreter" and choose your venv instead of ""env /usr/local/bin/python"" – Barak Avraham Jun 23 '20 at 12:52
  • Does this answer your question? [Setting the Python path for local project in VS Code without using the settings.json file](https://stackoverflow.com/questions/56825741/setting-the-python-path-for-local-project-in-vs-code-without-using-the-settings) – Ken Kinder Jun 23 '20 at 12:57
  • @BarakAvraham I don't use a virtualenv on that machne. – Matthias Jun 23 '20 at 13:07

1 Answers1

2

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

  • When I add the `FLASK_ENV` variable to the launch configuration then I get a "Exception has occurred: OSError: [Errno 98] Address already in use" error in the last line `app.run(debug=True)`. – Matthias Jun 23 '20 at 13:10
  • ok it is mean it is started to run the app put you maybe run another web app at the same port you can edit it to app.run(debug=True , port=4040) – Mohamed Farid Jun 23 '20 at 13:20
  • Sorry that was my fault, I had an old instance of this app running. However, adding the `FLASK_APP` env. variable does not fix my problem, still getting the "No module named run" error. – Matthias Jun 23 '20 at 13:33
  • try to replace all configurations with the edited in the answer – Mohamed Farid Jun 23 '20 at 13:41
  • That's it, thanks a lot. It even runs now when I remove the `--no-debugger` and `--no-reload` options from the `args` line. Thanks! – Matthias Jun 23 '20 at 13:51
  • @MohamedFarid I've tried every configuration possible and I just can't get it to work. I've been getting segmentation faults which makes it even harder to figure out what the issue is and so the debugger is just flat out broken. – OzzyTheGiant May 27 '22 at 16:43