Remote Debugging for Python
Prerequisites
- Install Visual Studio Code (VSCode)
- Download VSCode
- Install VSCode:
sudo apt install ~/Downloads/code_1.45.1-1589445302_amd64.deb
- Install the “Python” extension in VSCode
- Install Python debugging package:
pip install ptvsd
Configure Application for Debugging
- Edit the Main Application File
- Example configuration for
app.py
:import ptvsd # By default, remote debugging starts on port 5678 ptvsd.enable_attach(address=('0.0.0.0', 5678), redirect_output=True) print("Now ready for the IDE to connect to the debugger") ptvsd.wait_for_attach()
- Example configuration for
Load the Code into Visual Studio Code
- Transfer Code to Local Machine
- Use Rsync via SSH:
rsync -azP user@remote_host:/path/to/remote/code /path/to/local/code
- Use Rsync via SSH:
Configure Visual Studio Code to Connect to the Remote Debugger
- Start the Application with Debugging
- Ensure your application starts with the debugging configuration:
python app.py
- Ensure your application starts with the debugging configuration:
- Create
launch.json
in VSCode- Open the VSCode debug view and click on the gear icon to create a
launch.json
file. - Select “Remote Attach”.
- Update the
launch.json
configuration:{ "version": "0.2.0", "configurations": [ { "name": "Python: Remote Attach", "type": "python", "request": "attach", "connect": { "host": "remote_host", "port": 5678 }, "pathMappings": [ { "localRoot": "${workspaceFolder}", "remoteRoot": "/path/to/remote/code" } ] } ] }
- Open the VSCode debug view and click on the gear icon to create a
Generic Steps to Configure Remote Debugging for Any Python Application
- Install Necessary Tools and Extensions
- Ensure VSCode and the Python extension are installed.
- Install
ptvsd
ordebugpy
in your Python environment.
- Modify Application Code for Debugging
- Add debugging configuration in your main application file (e.g.,
main.py
orapp.py
):import debugpy debugpy.listen(("0.0.0.0", 5678)) print("Waiting for debugger attach") debugpy.wait_for_client()
- Add debugging configuration in your main application file (e.g.,
- Transfer Code to Local Machine
- Use tools like Rsync, SFTP, or any file synchronization tool to transfer code from the remote server to the local machine.
- Configure VSCode for Remote Debugging
- Create or update
launch.json
with appropriate settings:{ "version": "0.2.0", "configurations": [ { "name": "Python: Remote Attach", "type": "python", "request": "attach", "connect": { "host": "remote_host", "port": 5678 }, "pathMappings": [ { "localRoot": "${workspaceFolder}", "remoteRoot": "/path/to/remote/code" } ] } ] }
- Create or update
- Start Application with Debugging Enabled
- Run your Python application on the remote server with the debugging configuration:
python app.py
- Run your Python application on the remote server with the debugging configuration:
- Attach Debugger in VSCode
- Open the debug view in VSCode and start the “Python: Remote Attach” configuration.
This guide provides a generic way to set up remote debugging for Python applications using VSCode.
Article Information
- Author: Tony Wu
- URL: https://qwutony.github.io/wiki/python-generic-debugging/
- License: Free to share - NonCommercial - NoDerivatives - Attribution required (Creative Commons BY-NC-ND 4.0 License)