How to debug remote python code from local windows vs code - python

I want to debug python code (on remote linux) in local windows with VS code.
I did as follows:
In windows VS code, I can open remote linux python project using SSH.
Installed python debug tool ptvsd both in windows and remote linux.
Add code below in python project:
import ptvsd
ptvsd.enable_attach(address = ('$linux_ip', $port))
ptvsd.wait_for_attach()
Project launch.json:
{
// 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: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "$linux_ip",
"port": $port
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "$my_real_linux_ws"
}
]
}
]
}
Start run in remote linux.
Add break points in vs code, and run -> start debugging, then hit an issue as follows. I am confused that test.py is not in dir /c4_working/test.py but in dir /c4_working/python_code/test.py. And this file actually exists. So I am not sure why it would want to find the file in dir /c4_working/test.py? How would I fix it?

Have you read the documentation before asking your question?
A specially this part:
PS: strongly recommend to check remote-pdb as well.

Fixed this issue after creating a new launch.json file for this dir /c4_working/python_code.

Related

Cannot launch debugger for azure function app in vs code

I am trying to debug an azure function app in VSCode using Python in a Windows10 environment. Whenever I launch the debugger it hangs for a long period of time and then opens a message box that says
ECONNREFUSED 127.0.0.1:9091
There are a bunch of posts about this but none seem helpful/can solve the problem. Here is what I've tried:
uninstalling and re-installing different versions of
azure-function-core-tools using windows installer, npm and chocolatey
uninstalling and re-installing Azure Functions extension in VS Code
changing the extension bundle
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.3.0, 4.0.0)"
}
modifying local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python"
}
}
deleting C:\Users\admin.azure-functions-core-tools\Functions\ExtensionBundles
creating a function app from command line using "func init" and lauching debugger by running "func host start" in active venv
I am using Python38 and really have no idea what else to try. Any suggestions are welcome.
Thanks!
Cannot launch debugger for azure function app in VScode-
ECONNREFUSED 127.0.0.1:9091
This type of generic error may occur for a variety of reasons.
Need to check and modify:
First and foremost, check whether the versions of Azure functions core tools and Pip are upgraded to the current version:
To upgrade pip:
python -m pip install --upgrade pip
To install and upgrade azure-functions:
pip install azure-functions
Go to the below path,
view -> Command palette -> User Settings
Python functions, task runFunctionsHost windows command only work with powershell:
Set the integrated > default profile: Windows to PowerShell as PowerShell runtime host is functional with Python functions. It was previously set to "null".
The debug configuration is specified in your tasks.json and launch.json files in the .vscode folder.
As stated here , the default listening port in launch.json is set to 9091. Here, I updated it to "port: 7071" which is open for traffic on my function project, launched the files, and then executed the "Attach to Python Functions" debug task once again.
Under .VScode folder -> launch.json file, this configuration changes works for me.
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "python_modules./.bin/func",
"console": "integratedTerminal"
},
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 7071,
"preLaunchTask": "func: host start"
}
]
}
Added multiple debug points, debugged and triggered successfully as shown below:
Also Check here for more approaches given by #Hari Krishna
found the solution at:
https://github.com/Azure/azure-functions-core-tools/issues/3160#issuecomment-1266273749
I ran command func start in verbose mode
func start --verbose
from there it was clear that the process timed out when trying to download a new extension bundle. Most likely due to slow internet. I manually installed the new extension bundle:
https://functionscdn.azureedge.net/public/ExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/3.15.0/Microsoft.Azure.Functions.ExtensionBundle.3.15.0_any-any.zip
(the full path should be in the --verbose output) and extracted to
C:\Users[user name].azure-functions-core-tools\Functions\ExtensionBundles\Microsoft.Azure.Functions.ExtensionBundle\3.15.0
It now works. Thanks everyone for input.

Python debugger not responding in vscode

When I am debugging my python code from vscode, debugger shows no response and nothing on my terminal.
I have tested it with simple hello world code with a breakpoint. No output and breakpoint is never touched.
This was working fine a day before and only change I have done is installing a PYCharm(which I have uninstalled it now).
While if I execute the program without debugger then it executes without any issue.
launch.json screennshot below:
VS already updated:
Requirements.txt screenshot:
Launch.json file code
{
// 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",
"stopOnEntry": true,
"justMyCode": false
}
]
}
This is a bug in VScode. One workaround you can try is to add a requirements.txt file like this:
jedi==0.15.1
parso==0.5.1
isort==4.3.21
ptvsd==5.0.0a5
pyparsing==2.4.0
six==1.12.0
packaging==19.2
and save it to
%USERPROFILE%/.vscode\extensions\ms-python.python-2019.10.41019\
on windows
or
$HOME/.vscode/extensions/ms-python.python-2019.10.41019/
for mac and linux.
If that doesn't work, try to update the version of VScode. You may have an outdated version without the bug fixes. You could also try to add stopOnEntry and justMyCode to your launch.json file:
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
// Add these lines
"stopOnEntry": true
"justMyCode": false
},
}
You may need to add this because the default debugger will execute your file until it encounters an exception (or your program exits).

Why is my Vimspector debugging session not initializing?

I configured my vimspector.jsonfile and want to launch the debugger but when I do that it just says that it is initializing the debugger:
this is is my Vimspector.json:
{
"configurations": {
"test":{
"adapter": "vscode-python",
"configuration": {
"name": "this is a python test",
"type": "python",
"request": "launch",
"program":
"~/Documents/coding/python/terminal_game1/Terminal-Game/terminal_game.py"
}
}
}
}
Does anybody know what this means and how I can fix it?
Update:
After I've tried to use the debugpy-adapter the problem doesn't occur anymore and the debugger initializes correctly. But when trying to use the vscode-python adapter it still doesn't work even though I have installed its adapter via :VimspectorInstall vscode-python which ran successfully.
The adapter "vscode-python" is now declared legacy (No longer supported, please migrate your config), the supported adapter for python is now "debugpy".
so just replace vscode-python with debugpy in your .vimspector.json
This can be found on the plugin's Github page here.

Visual Studio Code unable to set env variable paths prior to debugging python file

I have a python project I need to debug
It comes with its own python installation, lets say in c:\project\python.exe and libraries, located in c:\project\libraries\..
It's launched with a cmd file C:\project\start.cmd
The cmd file executes another file called c:\projects\setenv.cmd which sets up paths to each library while the prompt is open
I'm trying to replicate this functionality in Visual Studio Code
I created a workspace and set it up so the following files are in the workspace .vscode folder:
settings.json
{
"python.pythonPath": "C:\\project\\python.exe"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"preLaunchTask": "shellCommand",
"console": "integratedTerminal"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "shellCommand",
"command": "C:\\project\\set_env.cmd ; pause",
"type": "shell"
}
]
}
When I open the main.py file which the start.cmd executes after setenv.cmd, and start debugging,
I can see that the task shellCommand is executed and the setenv.cmd runs, because of the pause statement.
The output I get is below:
C:\project>SET PYTHONPATH=C:\project\libraries\camera;C:\project\libraries\exewrappers
Press Enter to continue...:
However, the main.py file crashes on the first library import, which it cannot find
The library path is correct, but it seems that Visual Studio Code forgets the Path variable after setenv.cmd is executed
How can I make it so the env variables set up by setenv.cmd are still remembered when I'm debugging the main.py file?

VSCode Pyramid Debug

I am struggling to configure VSCode on a Linux machine to start debugging a pyramid app (via pserve) without any success.
Any guidance will be much appreciated.
I was successful to debug my Pyramid application using the program option under Linux using this VS code 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: Pyramid Application",
"type": "python",
"request": "launch",
"program": "FullPathToExecutableFileHere",
"args": [
"param1",
"${workspaceFolder}/XXX.ini"
],
"pyramid": true,
"jinja": true
}
]
}
By giving the full path to the executable file, and providing the parameters in the args section.
I can now set a break point in any python source file code.
Don't forget to set the path of the python interpreter to the specific python virtual environment needed by your application using CTRL + Shift + P

Categories

Resources