1

I've just started using VS Code with the Python plugin. I've set up a venv, launched code within that venv, installed all my necessary modules and updated my launch.json to launch flask apps as follows:

        {
        "name": "Python: Flask (0.11.x or later)",
        "type": "python",
        "request": "launch",
        "stopOnEntry": false,
        "pythonPath": "${config:python.pythonPath}",
        "program": "${workspaceFolder}/env/Scripts/flask.exe",
        "cwd": "${workspaceFolder}",
        "env": {
            "FLASK_APP": "${workspaceFolder}/main.py"
        },
        "args": [
            "run",
            "--no-debugger",
            "--no-reload"
        ],
        "envFile": "${workspaceFolder}/.env",
        "debugOptions": [
            "RedirectOutput"
        ]
    },

However when I launch the debugger for Flask in VS Code the following appears in my debug console:

ValueError: source code string cannot contain null bytes

However, if I just launch the app from the commandline doing:

set FLASK_APP=main.py
python -m flask

it works just fine. I'm sure it's something stupid I've done but I can't figure out what that is.

Note also that pythonPath points to my venv python installation.

5
  • I don't know what flask is, and I don't have time to look into it right now, but: "program": "${workspaceFolder}/env/Scripts/flask.exe" Is that right, or should it be a Python script or something? Commented Dec 22, 2017 at 9:08
  • According to this link: code.visualstudio.com/docs/python/debugging it should point at the flask executable. Commented Dec 22, 2017 at 9:29
  • Hmmm... And is there any way to run flask through the usual Python interpreter, instead of flask.exe? For example, do you have a file called flask.py? It does seem slightly unlikely to me that launch.json would have a field which can be either a binary or a script. Commented Dec 22, 2017 at 10:20
  • Yes, you can do python -m flask In either case, though, your app's entry point is defined as an env var, FLASK_APP. Commented Dec 22, 2017 at 11:34
  • You might call flask itself the entry point, I suppose it depends on your point of view. But my point is: try setting "program" to the Python script for flask, if you can find it. It must exist somewhere, if python -m flask works. Commented Dec 22, 2017 at 12:01

1 Answer 1

1

I am seeing the ValueError as well, and haven't found a proper solution for this, but have a workaround, using the "module": "flask":

{
    "name": "Python: Flask (0.11.x or later)",
    "type": "python",
    "request": "launch",
    "stopOnEntry": false,
    "pythonPath": "${config:python.pythonPath}",
    //"program": "${workspaceFolder}/env/Scripts/flask.exe",
    "module": "flask",
    "cwd": "${workspaceFolder}",
    "env": {
        "FLASK_APP": "${workspaceFolder}/main.py"
    },
    "args": [
        "run",
        "--no-debugger",
        "--no-reload"
    ],
    "envFile": "${workspaceFolder}/.env",
    "debugOptions": [
        "RedirectOutput"
    ]
}

It does, however, take some time to start the Flask app each time I start debugging, not sure if this is due to Flask being started via Python. Invoking flask.exe and python.exe -m flask from the command line seems to be evenly fast - notably faster than starting debugging in VS Code.

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

Comments

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.