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.

3
  • "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"" Commented Jun 23, 2020 at 12:52
  • Does this answer your question? Setting the Python path for local project in VS Code without using the settings.json file Commented Jun 23, 2020 at 12:57
  • @BarakAvraham I don't use a virtualenv on that machne. Commented Jun 23, 2020 at 13:07

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

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).
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)
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.
try to replace all configurations with the edited in the answer
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!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.