Debugging flask app results in "TypeError: compile() expected string without null bytes"
See original GitHub issueFrom @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
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Pygame compile() expected string without null bytes
It seems to work fine regardless of using parentheses or brackets, but I tried compiling it with brackets and it came up with...
Read more >Issue 26963: compile() expected string without null bytes
"compile() expected string without null bytes" I met this in python2.7.11, and python2.7.10 is fine. ENV: DOCKER:2.7.11-alpine, pecan, ...
Read more >Troubleshooting Amazon SageMaker Model Building ...
Information about common errors and how to resolve them when using Amazon SageMaker Model Building Pipelines.
Read more >Debugging — Emscripten 3.1.26-git (dev) documentation
The debug logs can be analysed to profile and review the changes that were made in each step. Note. The more limited amount...
Read more >Pexpect Documentation
modules for Python, Pexpect does not require TCL or Expect nor ... before property will contain all text up to the expected string...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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 thatvisualstudio_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.@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).