Setting up Python remote debugging in VSCode - python

I am having my remote(ubuntu 16.04) drive mounted on my local system(ubuntu 16.04) so that I can edit the source files by opening them in vscode.
Also, in the integrated terminal I can ssh to remote system and run the program using remote python interpreter which is installed on virtual environment like:
$ssh username#remoteip
$workon remotevirtualenv
(remotevirtualenv)$python source.py
I want to enable remote debugging so that if I run the debug/run the remote files, the vscode which is installed on my local system uses my remote python interpreter.
I went through the docs suggesting the use of ptvsd extension(which asks to have 2 copies of source files one on local, another one server) but I am not sure how to configure it in this scenario.
Appreciate the help. Thanks.
Edit 1:
As, I said I have gone through the docs but I am not clear how to configure in this scenario. e.g. docs say
In the source code on both computers, add the following lines, replacing my_secret with the appropriate passphrase to authenticate
remote debugging, and replacing address with the appropriate IP
address (or localhost) and port number:
ptvsd.enable_attach("my_secret", address = ('0.0.0.0', 3000))
But I have only one copy of source file i.e. on remote system. I have just mounted it on my local file system. So, should I give my local ip address or it should be remote system ip and which port number should I use as I am having only one copy of source and hence ptvsd, how is it going to communicate.
Also, in the configuration, what should I use for localRoot location and remoteRoot location.
3. {
"name": "Attach (Remote Debug)",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}",
"port": 3000,
"secret": "my_secret",
"host": "localhost"
}
Edit: Thanks to remote development extension in VSCode, remote debugging is super easy now.

You need to put the remote ip address in both ptvsd.enable_attach("my_secret", address = ('remote_ip_address', 3000)) and in launch.json:
{
"name": "Attach (Remote Debug)",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/home/user1/scripts/",
"port": 3000,
"secret": "my_secret",
"host": "remote_ip_address"
}
You also need to change the remoteRoot value to the path of the directory where the script is located in the remote machine (e.g. /home/user1/scripts/).
Finally, open an ssh connection: ssh -L 3000:localhost:3000, run your script in the remote machine and attach the debugger in the local machine.

Please see the official docs on how to do remote debugging.

Related

How to remotely debug Python with IntelliJ?

Is there a way for remotely debugging Python3 with IntelliJ? I couldn't find any options for it. With VS Code, it's as simple as having this file:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "172.18.0.5",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
"justMyCode": true
}
]
}
and everything works like a (py)charm!
How can i do it with IntelliJ?
I examined all of JetBrains' tutorials, but none seemed to fit what I was searching for.
IntelliJ IDEA and PyCharm both support Remote Debugging with Python, just follow the guide here:
https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html
If you are using IntelliJ IDEA, you may install the Python plugin first, by Settings > Plugins > Marketplace, inputting Python, and then clicking the Install button.
And here is the step to step guide on Remote debugging with the Python remote debug server configuration https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html#remote-debug-config:
Let IDEA/PyCharm host a debugger server on the machine your IDE runs on locally:
Note: This step is a lot different compared with VS Code(Sorry I know little about that tool), in VS Code it's using the package https://github.com/microsoft/debugpy, but it first will call python -m debugpy --listen 0.0.0.0:5678 ./myscript.py to create a debug server on the remote machine/docker, then the Client(here is VS Code) connect to it. But in IDEA/PyCharm it's just the opposite: Python Debug server is listening on the local machine, and the remote script will connect to the IDE's server.
From the main menu, choose Run| Edit Configuration.... The Run/debug configurations dialog opens. You have to click Add configuration on the toolbar, and from the list of available configurations, select Python Debug Server.
Please change the port to a different number between 0 and 65535, default value 0 will not work here, then remember the steps above the IDE host name area.
More info about the IDE host name: the user may specify another host if the debug server is not in the same machine (default is the localhost). In your case you want to connect it from a container, you may change it to the IP of your current machine.
You may also set up the Path Mapping: by clicking the folder selector and then selecting the real folder path as ${workspaceFolder}.
Then click OK button to save it, and then you click the run button to let the debug server run up and listen on remote code to connect to it, it will display something like this in the Debug tool window:
Starting debug server at port 12,345
Waiting for process connection…
Use the following code to connect to the debugger:
import pydevd_pycharm
pydevd_pycharm.settrace('some ip address', port=12345, stdoutToServer=True, stderrToServer=True)
Install and run debug server client in remote machine/container
On your container, execute the following one-time commands in the terminal(In case you are using IDE 2022.3):
pip3 install pydevd-pycharm~=223.8214.51
Then modify the Python file you want to debug by adding lines at the begging of the source file(THIS STEP IS MANDATORY), for eg:
#==============this code added==================================================================:
import pydevd_pycharm
pydevd_pycharm.settrace('some ip address', port=12345, stdoutToServer=True,
stderrToServer=True)
# ... The other codes remain as is
Then you may start remote debugging by just executing the below command in the terminal of the remote machine/container:
python3 myfile.py
Return to the local IDE and begin debugging
Just after step 2, now the local IDE will begin the debugging.
And the final result may look like this:

Attach a debugger to Azure Function App running in a container

I am curious to know if anyone successfully attached a debugger to a python script -
which is actually an Azure Function App running in a container.
I have all the VSCODE extensions but still can't seem to connect to the running container.
I've run the container from docker-compose.debug and from Bash cmd:
$ docker run -p 5678:5678 -it -e AzureWebJobsStorage="UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://host.docker.internal" nfunc:latest
NB I have to set an environment variable AzureWebJobsStorage for the storage.
My launch.json as follows:
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
What am I missing please ..
Can Functions Apps be debugged at all in a container?
Why does it not hit a breakpoint ?
Thank you
In this case, I believe you'll have to do the profiling. You will have to follow these:
If its not a blessed image, then first you would have to install SSH
if you want to get into the container.
Then you will have to make use of tools such as cProfile or other related python modules to profile the code.
Here is a documentation for windows application. You might want to take a look :
https://azureossd.github.io/2017/09/01/profile-python-applications-in-azure-app-services/index.html
This issue has been tracked : https://github.com/Azure/azure-functions-docker/issues/17

How to debug mpirun Python processes in Visual Studio Code on Ubuntu?

Create /home/bob/foobar.py in Visual Studio
Code with the VS Code Docker
Extension
import ptvsd
import time
ptvsd.enable_attach(address = ('0.0.0.0', 5678))
ptvsd.wait_for_attach()
time.sleep(2)
print("all righty then")
Set a breakpoint on the last line.
Debug|Add Configuration
In launch.json add to "configurations"
{
"name": "Python Attach (Remote Debug ptsvd default)",
"type": "python",
"request": "attach",
"pathMappings": [
{
"localRoot": "/home/bob", // You may also manually specify the directory containing your source code.
"remoteRoot": "/home/bob" // Linux example; adjust as necessary for your OS and situation.
}
],
"port": 5678, // Set to the remote port.
"host": "0.0.0.0" // Set to your remote host's public IP address.
},
$ python -m pip install --user --upgrade ptvsd
$ python foobar.py
Start the debugger with configuration "Python Attach (Remote Debug ptsvd default)". It stops at the breakpoint.
But if I run mpirun
$ mpirun --allow-run-as-root -np 2 -H localhost:2 -bind-to none -map-by slot -x PATH -mca pml ob1 -mca btl ^openib python ./foobar.py
I get the error socket.error: [Errno 98] Address already in use
Is there a way to assign multiple ports for an arbitrary number of processes in launch.json and in foobar.py?
This is not currently possible but has been added by the ptvsd team as a possible enhancement. Vote it up if this is important to you. Upvotes for this team seem to move features up the to-do list.

VScode: How to debug a python 2.7 cgi script?

I have lighttpd web server running on my Windows 7 pc using port 80. My web app calls python cgi scripts. I am trying to configure VScode to debug these python scripts when I click a button on one of my html pages. For example:
http://localhost/cgi-bin/instantlight.py?Input=&lightColor=blue
In my launch.json file I have:
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}",
"port": 80,
"secret": "my_secret",
"host": "localhost"
}
When I launch the debugger and click a button, my breakpoint is never hit. After a minute or two I get the error message "Debug adapter process has terminated unexpectedly"
What am I missing?
You don't need to attach to a running process as you're launching the server yourself locally (you only need attach if you're attaching to a server you don't start up yourself). So you can use the e.g. Python: Current File configuration.

Python remote debuging with PyCharm and ptvsd

I have a virtual machine running a python process and I want to debug it from my host.
My virtual machine ip is 192.168.10.20.
As per the instructions I set up my launch.json as
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "/Users/wrkspace",
"remoteRoot": "/home/wrkspace",
"port": 3000,
"secret": "my_secret",
"host": "192.168.10.20"
},
I have two files,one on my host machine and on my vm called test.py which is a helloworld asking for a user input
My host machine has this at the top of the file
ptvsd.enable_attach("my_secret", address = ('192.168.10.20'3000))
# Enable the line of source code below only if you want the application
to wait until the debugger has attached to it
#ptvsd.wait_for_attach()
While on my vm, where I want to debug
ptvsd.enable_attach("my_secret", address = ('192.168.10.20'3000))
# Enable the line of source code below only if you want the application
to wait until the debugger has attached to it
ptvsd.wait_for_attach()
Once I run my attached debugger and then I kick of the test on my vm by going python test.py. It looks like it does something but then fails with
There was an error in starting the debug server.
Error="code":"ENETUNREACH","errno":"ENETUNREACH","syscall":"connect","address":"192.168.10.20","port":3000}
There was an error in starting the debug server. Error = {"code":"ENETUNREACH","errno":"ENETUNREACH","syscall":"connect","address":"192.168.10.20","port":3000}
Is there something I'm missing?
A few steps to trouble shoot these:
1) test the debug port to make sure it is open using "nc -zv test.com 30302"
2) make sure your webserver is not reloading the app after debugger was connected to it. this is the option for flask: " use_reloader=False"

Categories