VS Code + Flask: ValueError: sourcecode string cannot contain null bytes - python

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.

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.

Related

Setting up Anaconda for VSC

I am trying to set up Python Anaconda for Visual Studio Code, I can set the interpreter to run as python however when I run it I get an error message saying: "The Python path in your debug configuration is invalid.", when I open the launch.json I get the following output:
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config.python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceRoot}",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
I am not entirely sure what I should change here to make it work?
Any ideas are very welcome as I am not able to find anything on this on google
Edit: This is how my editor looks: https://imgur.com/a/xoFmxc6

VS Code python debugger only works one time.

The debugger in VS Code in only working one time (either using debugging or start without debugging) and after that it won't start at all. I then have to restart VS Code and it works. I only have this issue since updating to v1.18.1.
{
"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"
]
},
I am seeing the same thing. Even with a completely fresh install. This is quite a show stopper, any pointers appreciated.
In the statusbar i can see python being running still and i have not managed to stop it besides restarting vsc.

How to set up Python in VS Code?

I'm new to python (and in coding in general). I'd like to ask some help to set up python on VS Code. I've tried to follow several guides but none of them were really helpful.
The following have been downloaded:
Python 3.6
VS Code
Python extensions
1) Install VS Code
2) Go to View > Command Palette
3) Type ext install and click on Install Extensions
4) Search for Python and install it
5) Reload VS
6) Start coding
This worked for me:
.vscode/settings.json:
{
"python.linting.pylintEnabled": false,
"python.pythonPath": "python.exe"
}
.vscode/tasks.json:
{
"version": "2.0.0"
}
.vscode/launch.json:
{
"version": "2.0.0",
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceRoot}",
"console": "internalConsole",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
}
]
}
You can replace "program": "${file}", with "program": "${workspaceRoot}/main.py", to run your main file no matter which file you've selected but I found that that makes errors like syntax errors sometimes not display correctly if at all.
Breaking on exceptions
Press CTRL + SHIFT + D
In the BREAKPOINTS panel, click on Uncaught Exceptions
enter image description here

How to use visual studio code to debug django

I'm new at django development and come from desktop/mobile app development with Xcode and related IDE.
I have to use Django and I was wondering if there was an efficient way to debug it using Visual Studio Code (or Atom).
Any help related to Django IDE would be helpful too.
For VSCode (full disclosure, I'm one of the VSCode developers) try installing the Python extension to get started.
This documentation covers debugging Django. There should be a included debug configuration or you can add your own to the launch.json file:
{
"name": "Django",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config.python.pythonPath}",
"program": "${workspaceRoot}/manage.py",
"args": [
"runserver",
"--no-color",
"--noreload"
],
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput",
"DjangoDebugging"
]
}
The Python extension also provide many other features that you may find useful.
VSCode has an official tutorial explaining this:
https://code.visualstudio.com/docs/python/tutorial-django
There are several steps that need to be taken, which I don't all want to write out manually, since there are quite some steps, but I'll try to summarize what needs to be done:
The text below is basically a partial copy of the above tutorial, I am not claiming I came up with this myself.
1. Make sure to check out the prerequisites (use VS Code Python extension, install Python on local machine) link to docs
2. Use Python virtual environment link to docs
Besides using a Python virtual environment, you also need to select the Python executable inside this virtual environment as the interpreter in VS Code. This can be done like so:
In VS Code, open the Command Palette (View > Command Palette or (Ctrl+Shift+P)). Then select the Python: Select Interpreter
Then you select the Python executable inside your virtual environment, which you can recognize by it's path.
3. Create debugger lauch profile
as described here, in the documentation
upper left of the VS Code window)
4. Now you can start debugging
this part of the documentation will give you an introduction on how to do that
Only experimental configuration works for me.
{
"name": "Django",
"type": "pythonExperimental",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
Standard config causes Unverified breakpoint issue.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"runserver"
],
"django": true
},
{
"name": "Django: makemigrations",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"makemigrations"
],
"django": true
},
{
"name": "Django: migrate",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"migrate"
],
"django": true
},
]
}
Nothing worked for me until I had disabled auto reload (--noreload as an argument is crucial, not really sure why it causes problem with debugging)

How to configure Visual Studio Code to debug Django app in a virtualenv?

I would like to have possibility to do a debugging of Django application in Visual Studio Code. I have a virtualenv, made a change in launch.json file that look like this:
{
"name": "Django",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${workspaceRoot}/.venv/bin/python2.7",
"program": "${workspaceRoot}/mysite/manage.py",
"args": [
"runserver"
],
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"DjangoDebugging",
"RedirectOutput"
]
},
put several break points in the code and run it. Unfortunately, the execution is not stopped on the line with break points. I tried the same without virtualenv and everything worked perfectly.
Please, point out what I am doing wrong here.
For me, the following 2 changes worked
Add an absolute path for pythonPath
Use the "--noreload" option while starting the project
Here's the relevant part of my config
{
"name": "Django",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "/Users/xyz/Documents/dev/my_project/my_project_env/bin/python",
"program": "${workspaceRoot}/manage.py",
"args": [
"runserver",
"0.0.0.0:8080",
"--noreload"
],
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput",
"DjangoDebugging"
]
},
1) Press CTRL + ,
2) Select Workspace Settings
3) Add the following line in the settings file opened.
"python.pythonPath": "path_to_your_env"
You're done!
This official tutorial worked in my case.
Just needed to open the "command palette" of VS Code and select my Python Interpreter to the virtual environment:
Full reference: https://code.visualstudio.com/docs/python/tutorial-django#_create-a-project-environment-for-the-django-tutorial

Categories