Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask debugger configuration fails (Window) with "could not import" message #1634

Closed
kraigb opened this issue May 7, 2018 · 6 comments · Fixed by #1641
Closed

Flask debugger configuration fails (Window) with "could not import" message #1634

kraigb opened this issue May 7, 2018 · 6 comments · Fixed by #1641
Assignees
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster
Milestone

Comments

@kraigb
Copy link

kraigb commented May 7, 2018

2018.4.0 release.
Windows 10

Also reported as microsoft/vscode#48972.

Repro steps:

  1. Create a "hello.py" with code like the following:
    from flask import Flask
    app = Flask(__name__)

    @app.route('/')
    def hello_flask():    
        return '<html><body>Hello, <strong>Flask</strong>!</body></html>'

    if __name__ == '__main__':
        app.run('localhost', 5555)
  1. Run the app under the Python: Current File configuration. Result: success.
  2. Run the app under the Python: Flask configuration. Result:
    D:\py\Flask>cd d:\py\Flask && cmd /C "set "FLASK_APP=D:\py\Flask/app.py" &&     set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" &&     d:\py\Flask\env\Scripts\python.exe
    C:\Users\kraigb\.vscode\extensions\ms-python.python-2018.4.0\pythonFiles\PythonTools\visualstudio_py_launcher.py d:\py\Flask 64579 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput -m flask run --no-debugger --no-reload "
     * Serving Flask app "D:\py\Flask/app.py"
     * Environment: production
       WARNING: Do not use the development server in a production environment.
       Use a production WSGI server instead.
     * Debug mode: off
    Usage: python -m flask run [OPTIONS]

    Error: Could not import "D".

Seems like a problem parsing the path.

@DonJayamanne DonJayamanne added bug Issue identified by VS Code Team member as probable bug needs verification area-debugging labels May 7, 2018
@DonJayamanne
Copy link

DonJayamanne commented May 8, 2018

@kraigb
I've identified the problem(s),
Problem 1
The name of the file must be app.py or you need to modify launch.json to change app.py to hello.py
Problem 2
There's a breaking change in the latest version of Flask.
We'll fix the extension to accommodate this change.

Solution Change the setting "FLASK": "${workspaceFolder}/app.py" to:
"FLASK": "hello.py" in the launch.json file

@DonJayamanne DonJayamanne self-assigned this May 8, 2018
@DonJayamanne DonJayamanne added this to the May 2018 milestone May 8, 2018
@DonJayamanne DonJayamanne added needs PR info-needed Issue requires more information from poster and removed needs verification labels May 8, 2018
@harleydk
Copy link

harleydk commented May 9, 2018

@DonJayamanne I too am having this issue. Is there a suggested work-around? I'm downgrading Flask to accomodate for the breaking change in the latest version, as you mention, but that's not doing me any good.

My original point of entry was https://github.com/DonJayamanne/pythonVSCode/wiki/Debugging:-Flask, did me a lot of good. I gather it's originally from https://keathmilligan.net/debugging-a-flask-0-11-app-in-visual-studio-code/, but doesn't mention run.py not being needed (Quote "previously, you would need to add a “run.py” script to your project for Windows, but that is no longer needed"). I don't have it and am confused if I should?

Fwiw here's my configuration in launch.json:

 "configurations": [
        {
            "name": "Flask",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "module": "flask.cli",
            "cwd": "${workspaceRoot}",
            "env": {
                "FLASK_APP": "${workspaceRoot}app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }

Thanks for your consideration.

@DonJayamanne
Copy link

Change "FLASK_APP": "${workspaceRoot}app.py" to "FLASK_APP": "app.py", where app.py is your application module in flask.

@harleydk
Copy link

harleydk commented May 9, 2018

@DonJayamanne Thanks a lot, appreciate it.

In my specific case I had my app.py in a sub-folder, so my flask-app environment variable is

"FLASK_APP": "my_awesome_api_folderName\\app.py"

@sergioamr
Copy link

It seems that the windows format C:\app_folder is not correct.
I replaced ${workspaceFolder} with /c/app_folder/ in the launch.json and it seems to work properly.

@pskorupinski
Copy link

This setup works for me:

        {
            "name": "Python: Flask (0.11.x or later)",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "project.server",
                "FLASK_ENV": "development"
            },
            "args": [
                "run"
            ]
        },

Where Flask app is in the file project/server/__init__.py

@lock lock bot locked as resolved and limited conversation to collaborators Jul 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants