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 Empty Project.
In this tutorial, the project name is
Preparing an example
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)
Using a remote interpreter
Configuring a remote interpreter on Windows
Note that any remote interpreter will do.
Click Ctrl+Alt+S to open the Settings dialog on the Windows machine (the whole process is described in the section Accessing Settings).
Next, click the Project Interpreter node. On this page, click the gear button (), and choose the remote interpreter:
The Add server dialog box opens. There, enter the server name (let it be
MySFTPConnection) and choose the server type from the drop-down list. Naturally, it should be SFTP. Next, the deployment settings dialog opens for
MySFTPConnection. In the Connection tab, enter the following::
- In the field Name, type the connection name. Here it's MySFTPConnection.
- In the Type field, choose the connection type. Here it's SFTP.
- In the SFTP host field, enter the address of your Mac computer (for example, IP).
- Type user name on the Mac computer.
- As the authentication type is set to Password, choose this option and enter your Mac password.
Next, click the Mapping tab. The Local path field is set by default. You have to specify the path on the server
MySFTPConnection. To do that, click the browse button () next to the Deployment path on server 'MySFTPConnection' field. The dialog box that opens shows the contents of your Mac computer:
Now your remote interpreter is ready.
Deploying your application to a remote host
Debugging your application
Note that debugging actually takes place on the Mac computer!
Using the Python Remote Debug configuration
Let's do same not with the remote interpreter, as it has been done in the first part of this tutorial, but with the dedicated run/debug configuration, namely, Run/Debug Configuration: Python Remote Debug Server.
Creating run/debug configuration
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.
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('<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 . In the dialog box that opens, click , and in the Add Server dialog select the connection type (here SFTP) and enter its name (here
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 Remote Debugging with PyCharm, 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 you'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.