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

Debugging flask app results in "TypeError: compile() expected string without null bytes" #145

Closed
DonJayamanne opened this issue Nov 14, 2017 · 6 comments
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug

Comments

@DonJayamanne
Copy link

DonJayamanne commented Nov 14, 2017

From @nicebits on July 18, 2017 4:24

Environment data

VS Code version: 1.14.1
Python Extension version: 0.6.7
Python Version: 2.7.13
OS and version: Windows 10 x64

Actual behavior

Debugging a simple flask app results in the error message "TypeError: compile() expected string without null bytes" being printed to the debug console.

Expected behavior

The debugger to attach and the flask app to begin running a HTTP server.

Steps to reproduce:

  • Install Visual Studio Code
  • Install python
  • pip install flask
  • Update launch.json with the correct pythonPath and program settings for python.exe and flask.exe, respectively
  • Debug -> Start Debugging
  • Select "Flask" as the debugging configuration
  • Press continue (f5)
  • Error message is printed and debugging does not start

This is reproducible on a completely blank Windows 10 installation.

Settings

Your launch.json (if dealing with debugger issues):

    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        },
        {
            "name": "PySpark",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "osx": {
                "pythonPath": "${env:SPARK_HOME}/bin/spark-submit"
            },
            "windows": {
                "pythonPath": "${env:SPARK_HOME}/bin/spark-submit.cmd"
            },
            "linux": {
                "pythonPath": "${env:SPARK_HOME}/bin/spark-submit"
            },
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        },
        {
            "name": "Python Module",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "module": "module.name",
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        },
        {
            "name": "Integrated Terminal/Console",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "",
            "console": "integratedTerminal",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit"
            ]
        },
        {
            "name": "External Terminal/Console",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "",
            "console": "externalTerminal",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit"
            ]
        },
        {
            "name": "Django",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${workspaceRoot}/manage.py",
            "cwd": "${workspaceRoot}",
            "args": [
                "runserver",
                "--noreload"
            ],
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput",
                "DjangoDebugging"
            ]
        },
        {
            "name": "Flask",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "program": "c:\\python27\\scripts\\flask.exe",
            "cwd": "${workspaceRoot}",
            "env": {
                "FLASK_APP": "${workspaceRoot}/ssotest.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        },
        {
            "name": "Flask (old)",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${workspaceRoot}/run.py",
            "cwd": "${workspaceRoot}",
            "args": [],
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        },
        {
            "name": "Pyramid",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "args": [
                "${workspaceRoot}/development.ini"
            ],
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput",
                "Pyramid"
            ]
        },
        {
            "name": "Watson",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${workspaceRoot}/console.py",
            "cwd": "${workspaceRoot}",
            "args": [
                "dev",
                "runserver",
                "--noreload=True"
            ],
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        },
        {
            "name": "Attach (Remote Debug)",
            "type": "python",
            "request": "attach",
            "localRoot": "${workspaceRoot}",
            "remoteRoot": "${workspaceRoot}",
            "port": 3000,
            "secret": "my_secret",
            "host": "localhost"
        }
    ]
}

Your settings.json:

<Removed>

Logs

Output from Python output panel

Output from Console window (Help->Developer Tools menu)

Copied from original issue: DonJayamanne/pythonVSCode#1095

@DonJayamanne
Copy link
Author

From @KillyMXI on August 5, 2017 17:42

I've got an issue with debugging Flask too.
Error message was: TypeError: source code string cannot contain null bytes
(I can run it from console, so it's nothing about null bytes in my code.)

I made the following changes to make it work:

// "program": "${workspaceRoot}\\project_env\\Scripts\\flask.exe",
"module": "flask",

Run flask as module instead of script (not sure if it ever able to work in the way default config is suggesting!)
(It still takes some time to run though, but that isn't something unexpected from debugger.)

Setting "console": "integratedTerminal" helps to track down issues with debugger.
I found that visualstudio_py_launcher.py supports for -m option, but how I supposed to use it? Documentation has no mention of it. "module" key is found by trial and error (well, almost - there is config auto-completion at least). Still I have no idea how -c argument can be set.

Upd: found Debugging Flask page, with important note for Windows users. RTFM after facing some strange issues isn't a good workflow :(
"module": "flask" should be cross-platform and, therefore, better option.

@brettcannon brettcannon added awaiting 1-verification area-debugging bug Issue identified by VS Code Team member as probable bug labels Nov 14, 2017
@DonJayamanne
Copy link
Author

@nicebits are you still experiencing this issue. Please could you provide a sample code.

I can confirm this work as described by @KillyMXI (thanks @KillyMXI , I'll update the documentation and change the default launch config).

@DonJayamanne
Copy link
Author

Created a new issue to ensure the launch.json is updated accordingly.
#573

@SowingSadness
Copy link

@DonJayamanne hi! Thanks for direction. I have same issue with pyramid debugging.
Windows 10

{ "name": "Python: Pyramid 2", "type": "python", "request": "launch", "stopOnEntry": true, "pythonPath": "${workspaceFolder}/../pyramid2/Scripts/python.exe", "program": "${workspaceFolder}/../pyramid2/Scripts/pserve-script.py", "console": "integratedTerminal", "cwd": "${workspaceFolder}", "args": [ "${workspaceFolder}/development.ini" ], "debugOptions": [ "RedirectOutput" ] },
But VSCode don't detect source file if camel case is in path :(

@brettcannon
Copy link
Member

@SowingSadness can you open a new issue?

@SowingSadness
Copy link

@brettcannon
ready — #1010

@lock lock bot locked as resolved and limited conversation to collaborators Jul 11, 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
Projects
None yet
Development

No branches or pull requests

3 participants