PyCharm remote debugging quick start

1. Method

1. Configure the remote interpreter (simple and efficient, highly recommended!!!)

 Requirements: Access the remote server from the local machine through SSH. Use any predefined port to access the local machine from the remote server. It is best to turn off network proxy services such as VPN .

FAQ: pycharm can be run using a remote interpreter, but cannot be debugged

Execute remotely alone:

python -u /root/.pycharm_helpers/pydev/pydevd.py --multiproc --client 0.0.0. 0 --port 37990

 Error reported:

Could not connect to 0.0.0.0: 37990
Traceback (most recent call last):
  File "/root/.pycharm_helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 456, in start_client
    s.connect((host, port))
ConnectionRefusedError: [Errno 111] Connection refused
Traceback (most recent call last):
  File "/root/.pycharm_helpers/pydev/pydevd.py", line 2173, in <module>
    main()
  File "/root/.pycharm_helpers/pydev/pydevd.py", line 2055, in main
    dispatcher.connect(host, port)
  File "/root/.pycharm_helpers/pydev/pydevd.py", line 1826, in connect
    self.client = start_client(self.host, self.port)
  File "/root/.pycharm_helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 456, in start_client
    s.connect((host, port))
ConnectionRefusedError: [Errno 111] Connection refused

Problem cause analysis:
Requirements: Access the remote server from the local machine through SSH, use any predefined port to access the local machine from the remote server.

2. Use Python to remotely debug server configuration (code intrusion, not very recommended)

 Select Run | Edit Configuration… from the main menu. The Run/Debug Configuration dialog box opens. You have to click on the toolbar and select pythondebug Server from the list of available configurations.

Enter a name for the run/debug configuration, set it to MyRemoteServer; specify the port number of the machine where the IDE is running (12345 in this case) and the IDE hostname (172.20.208.95 in this case); the remote debugging server will use these parameters to access it.

According to the reminder of the IDE interface, install the corresponding pydevd-charm; execute pydevd_pycharm.settrace

Map a path on the local machine to a path on the remote machine:

Steps for usage:

1. After the IDE is started, it will show that it is in the listening state, and then set a breakpoint in Pycharm.

2. Then run the remote py program and execute it using the command line (not in the IDE, but in the remote command execution)

Modify the source code file as follows:

import math
#==============this code added==================================================================:
import pydevd_pycharm

pydevd_pycharm.settrace('172.20.208.95', 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)

As above, you can see that this method is intrusive to the code!

Note: If you deploy the program to a remote location, you need to change the localhost in the Remote Debug configuration to the IP address of the host where Pycharm is installed, and change the localhost in PycharmRemoteDebug.py to the same IP address.


Use Pycharm's Deployment function to map remote and local code;
when a breakpoint is hit, the client program is in a tentative state;
if the Remote Debug Server is not turned on, the client will be stuck when running;
when you do not need to use Remote Debug, be sure not to start the program When importing the PycharmRemoteDebug module;

Summary: Therefore, this solution must ensure that the remote environment can actively connect to the local environment of your IDE!
 

2. Introduction to remote debugging principles

If the program is deployed remotely, to obtain the program running status locally and perform breakpoint debugging, it is necessary to connect to the program and communicate; in the process of using Pycharm for remote debugging, Pycharm acts as a server.

First, configure Pycharm Run/Debug Configures and specify some attributes of the Pycharm installation side, such as the IP address and port number of the host where Pycharm is located.

Then, start Pycharm's remote debugging. At this time, Pycharm is in a listening state, waiting for connections from programs running independently of the IDE.

Secondly, when the remote program first starts, it needs to connect to Pycharm based on the configuration information in Pycharm Debug Configures.


Finally, after the connection is successful, when the remote client runs to the breakpoint set in the local Pycharm, the execution of the program will be paused at the breakpoint, and when the local Pycharm hits the breakpoint, you can see the current running content of the remote program. status, call stack and other information and conduct next-step tracking and step-by-step debugging.

The source code project debugged in local Pycharm should be consistent with the source code of the program running remotely.

The configuration of Remote Debug Configure in Pycharm must ensure that it can be connected remotely.

Since the remote client uses the pydevd module provided by Pycharm to connect to the local Pycharm remote Debug, the communication links between the two follow Pycharm's custom protocol; therefore, we do not need to care about the details of both ends when Pycharm sets breakpoints and the remote client hits the breakpoint. For the implementation and processing process, we only need to ensure that our Debug Configure is valid; then use breakpoints to temporarily pause the program where needed, and analyze the current program status to find out the problem.
 

Guess you like

Origin blog.csdn.net/JineD/article/details/133187483