How to use visual studio code to debug django - python

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)

Related

Problem in debugging django project using VS code

I have a problem while debugging a django project using VS code, the problem that nothing happened when I click to debug button, I can launch my script just by tapping in terminal python manage.py runserver.
Here is my launch.json file, and note please that I tried a lot of examples, and still the same problem:
{
"version": "0.2.0",
"configurations": [
{
"name": "Django",
"python": "C:/Users/msekmani/Desktop/dashboard_project/venv/Scripts/python.exe",
"type": "python",
"request": "launch",
"program": "C:/Users/msekmani/Desktop/dashboard_project/IPv2/src/manage.py",
"console": "internalConsole",
"args": ["runserver"],
"django": true,
"justMyCode": true,
},
]
}
I am using python version 3.6 and for the OS is Windows.
Note please that I also tried to creat a Python debug and also it's not working also, and here is my launch.json script:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
I find a solution to solve this problem by upgrade the python version to 3.7, I don't have any idea about the problem happened in version 3.6, by the way, the upgrade python version is the solution.

'No module named flask' in VSCode even when I have installed flask

I have my Flask installed in my main directory for playing with Flask projects and I don't want to change it (its name is tryFlask).
My project is in one of the subdirectories to tryFlask (main_project).
How should I config my launch.json so that it doesn't throw errors when I try to debug?
Current config which throws:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "my_app.py",
"FLASK_ENV": "tryFlask",
"FLASK_DEBUG": "1"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true,
"justMyCode": true
}
]
}
And yes I have already checked "FLASK_APP": "my_project/my_app.py". Doesn't work. Neither does reinstalling dependencies or even choosing different interpreters from venv's bin subdirectory.
If you want to use flask with vscode, following this tutorial is a good start.
According to the question in your post, if you want to specify the python interpreter when debugging, you can add the python configuration to launch.json
Simple example:
{
// 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: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
// Modify the python interpreter with flask installed on your machine
"python": "C:\\WorkSpace\\pytest10\\.venv\\Scripts\\python.exe"
// "python": "${workspaceFolder}/.venv/Scripts/python.exe"
}
]
}
For more launch.json configuration, you can check this link.

VS Code: ModuleNotFoundError when using debug

I develop a Django project
I ma newbie in Django and VS Code so I "debug" using print() and console.log() which is not efficient...
so I really need to be able to debug my code and follow variable value during execution but when I try to run debug
I read the VS Code doc about django
so I have set the launch.json like that:
{
// 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 : Fichier actuel",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload"
],
"django": true
},
]
}
I select Python: Django in the debug configuration list and run my project and have ModuleNotFoundError: No module named 'simple_history' and other...
I don't understand because my project works so tierce app are correctly installed in my env
It wonder if it could be link to another problem I have since I start using python/VS Code and was not able to fix (I already post on this problem: python can't load on VS Code):
The environment variable 'Path' seems to have some paths containing the '"' character. The existence of such a character is known to have caused the Python extension to not load. If the extension fails to load please modify your paths to remove this '"' character.
Someone can help me making debug operational?
thanks !

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 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