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
Related
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.
I did python debugging in VS code.
The following is the launch.json file:
{
// 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",
"stopOnEntry": false,
"python": "${command:python.interpreterPath}",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions":[
"RedirectOutput"
],
"console": "integratedTerminal"
}
]
}
The following is settings.json file:
{
"python.pythonPath": "c:\\Users\\susan\\Documents\\PythonScripts\\venv\\Scripts\\python.exe",
// to fix 'Timeout waiting for debugger connections'
"python.terminal.activateEnvironment" : false
}
When I debug the python script in VS code, I got Time out waiting for launcher to connect and cannot debug the python script.
May I know how can I solve this issue?
Its very simple. Open the launch.json file and add the following into it:
{
"name": "Python: Debug Console",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
}
Then save and exit it. Whatever you do, DO NOT clear the text already in there or else it may make it worser
If for whatever reason "internalConsole" isn't a good solution:
in your shell script:
export PROCESS_SPAWN_TIMEOUT=30
or just hack the code directly (will be reverted if you update the extension):
.vscode-server/extensions/ms-python.python-2022.18.2/pythonFiles/lib/python/debugpy/adapter/launchers.py[161]:
change:
timeout=(None if sudo else common.PROCESS_SPAWN_TIMEOUT)
to:
timeout=30,
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
I have been using Visual Studio Code for a few years and I find it a really good IDE. I program in Python more than any other language.
It really bothers me that I did not get around to using the IDE's debugger (even though it's a feature).
Error when pressing the debug play button:
Failed to launch the Python Process, please validate the path 'export PATH=$PATH:/Library/Frameworks/Python.framework/Versions/2.7/bin/python'
Checking my PYTHONPATH:
My launch.json file:
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "/workspace/h5-automation/",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
}
Any ideas / suggestions?
If you are on a *nix system, launch the terminal and type which python to get the Python path, copy it and paste it as the value of pythonPath in the launch.json file.
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