Remote Debugging with PyCharm
What to do, if the interpreter you are going to use, is located on the other computer? Say, you are going to debug an application on Windows, but your favorite interpreter is located on Mac... With PyCharm, it's not a problem.
Before you start
Make sure that you have an SSH access to Mac computer!
Creating a project
On Windows, create a pure Python project, as described in the section Creating Pure Python Project.
In this tutorial, the project name is
Preparing an example
Add a Python file to this project (Alt+Insert - Python File). Then, type the following code:
import math class Solver: def demo(self, a, b, c): d = b ** 2 - 4 * a * c if d > 0: disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 elif d == 0: return -b / (2 * a) else: return "This equation has no roots" if __name__ == '__main__': solver = Solver() while True: a = int(input("a: ")) b = int(input("b: ")) c = int(input("c: ")) result = solver.demo(a, b, c) print(result)
Creating a deployment configuration for a remote interpreter
Add a remote interpreter
Add new remote interpreter as described in Configuring Remote Interpreters via SSH.
Once you create the remote interpreter, the corresponding deployment configuration is created. To preview it, click Ctrl+Alt+S to open the Settings dialog window on the Windows machine, then click the Build, Execution, Deployment node and the Deployment node.
You can accept all default settings or alter them, if needed. For this example, let's use a meaningful name for your deployment configuration, for example, "MySFTPConnection". You can also click the Autodetect button near the Root path field to configure a root folder according to the user home folder on the target server.
Now your deployment configuration is ready.
Deploying your application to a remote host
Next, your application must be deployed to the remote host. Let's do it. On themenu, point to node, and then choose :
File Transfer tool window appears, with the balloon showing the number of transferred files.
Debugging your application
Now you are ready for debugging! Right-click the editor background and choose the(here ).
Note that debugging actually takes place on the specified remote server.
Using the Python remote debug server configuration
You can also enable remote debugging with the dedicated run/debug configuration, namely, Run/Debug Configuration: Python Remote Debug.
Creating run/debug configuration
On the main menu, choose Run/Debug Configurations Dialog opens. You have to click on the toolbar, and from the list of available configurations, select Python Remote Debug..
Enter the name of this run/debug configuration - let it be MyRemoteMac. Specify the port number (here
12345), and in the field Local host name change
localhost to the IP address of the Windows computer, where the IDE is running, by which the remote debug server can be accessible.
Don't forget to specify the path mappings! You have to map the local path on Windows to the remote path on Mac:
Then look at the dialog box. You see the message:
Let's do what is suggested there and change the source code. First, let's copy the
pycharm-debug-py3k.egg file to the project root. Second, let's change the
quadratic_equation file as follows:
import math #==============this code added==================================================================================: import sys sys.path.append("pycharm-debug-py3k.egg") import pydevd pydevd.settrace('<IP address of the Windows machine>', port=12345, stdoutToServer=True, stderrToServer=True) #=============================================================================================================== class Solver: def demo(self, a, b, c): d = b ** 2 - 4 * a * c if d > 0: disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 elif d == 0: return -b / (2 * a) else: return "This equation has no roots" if __name__ == '__main__': solver = Solver() while True: a = int(input("a: ")) b = int(input("b: ")) c = int(input("c: ")) result = solver.demo(a, b, c) print(result)
Creating a SFTP connection
First, create the folder on Mac, where the file
quadratic_equation.py should be uploaded. To do it, in the Terminal create the directory
Now, considering that we should upload this code, let's create a connection profile. To do it, on the main menu, choose Add Server dialog select the connection type (here SFTP) and enter its name (here
In the Connection tab, specify the SFTP host (address of the Mac computer), user name and password (for Mac).
Note that the user should have an SSH access to Mac computer!
Next, click Mappings tab, and enter the deployment path in server. The server is
MySFTPConnection, so click the browse button and select the required folder. Note that the browse button shows the contents of Mac! Apply changes and close the dialog.
Deploying files to Mac
Next, you have to deploy the two files (copy of
quadratic_equation.py) from Windows to Mac.
Then on Windows, in the Project Tool Window, select the two files (
quadratic_equation.py), right-click the selection and choose Upload to MySFTPConnection. The files from Windows will be uploaded to Mac, which is reflected in the File Transfer tool window:
Launching the Debug Server
Choose the run/debug configuration created in the section Using the Python remote debug server configuration, and click :
The Debug Tool Window shows the Waiting for process connection.. message, until you launch your script on Mac, and this script will connect to the Debug Server.
Launching file on Mac
Next, you have to launch the
quadratic_equation.py file on Mac. To do that, in the Terminal, enter the following command:
Debugging on Windows
Now lo and behold! Your code on Windows shows the results of the inline debugging and hit breakpoint; the Debug tool window switches to the Debugger tab and shows the stepping toolbar, frames and variables with their values (entered on Mac):
Note that the code is actually executed on Mac, but debugged on Windows!
In order to debug with a remote interpreter, you have to start your program through PyCharm, which is not always possible. On the other hand, when using the Debug Server, you can connect to a running process.
Compare the two approaches.
In the first case, we
- configured a remote interpreter on Windows.
- deployed the script to Mac.
- debugged the script. Note that we've started the debugger session on Windows, but actual debugging takes place on the Mac computer.
In the second case, we
- created a debug configuration (Debug Server) on Windows.
- launched the Debug Server on Windows. The Debug Server waits for connection.
- executed the Python script on Mac. The script connects to the Debug Server.
- debugged the script on Windows.
It's up to you to choose the way you debug your scripts.