Use pycharm to remotely debug the program & connect to the linux server to debug the program (pro-test is feasible!!)

Table of contents

The premise of method 1 and method 2: create a deployment configuration for the remote python interpreter

Remote debugging method 1: Use a remote python interpreter

Remote debugging method 2: Remote debugging using python remote debugging server settings

Additional Notes: Possible problems. .


Tip: Look out for the pictures in my tutorial! ! ! There are text descriptions on the pictures, and the explanations are very careful. Follow the steps to ensure success! I suggest that you follow the method 1 in my tutorial for remote debugging~~

Note: Pycharm community version does not support remote debugging function, please download pycharm professional version !

With PyCharm you can debug your application using an interpreter located on another computer (the server), for example, on a web server or a dedicated test computer. PyCharm provides two remote debugging methods:

Remote debugging method 1: Remote debugging using a remote Python interpreter (recommended)

Description: Use this method to take advantage of the extended debugging capabilities available on the remote computer.

Requirements: SSH access from local machine to remote server.

Remote debugging method 2: Remote debugging using Python  remote debugging server configuration

Description: Use this method to integrate the debugging process into a series of processes running on the remote server. This can be helpful when you cannot explicitly run the application for debugging or when certain preparatory tasks are required.

Requirements: SSH access from local computer to remote server, access from remote server to local computer using any predefined port.

Here is a link to the official website tutorial: Using PyCharm | Remote Debugging PyCharm Documentation (jetbrains.com) , my tutorial refers to the official document, you can use it with confidence!

The premise of method 1 and method 2: create a deployment configuration for the remote python interpreter

1. Toolbar -> Deployment -> Configuration

 2. Create an SFTP connection

On the Connection tab, specify the computer's SFTP host (the address of the remote computer), username, and password.

 3. Set the mapping path (local path, deployment path)

 4. Deploy the file to the remote computer (server)

Check the File Transfer dialog window to make sure the file from your local computer has been uploaded to the remote server.

Remote debugging method 1: Use a remote python interpreter

1. Configure the project interpreter (select the server-side python interpreter via SSH)

 2. Debug execution and view the output

 Note that debugging is actually done on the specified remote server.

Remote debugging method 2: Remote debugging using python remote debugging server settings

1. Click Edit Configuration in the main menu and select Python Debug Server from the list of available configurations. (See the picture below for details)

2. Specify the running IDE host name (local ipv4 address) and port number (you can set the unoccupied port arbitrarily, 53100/777/... can be used, here is 6666), the remote debugging server will use these parameters to Access it; maps a path on the local computer to a path on the remote computer (server).

 The local ipv4 address query method is shown in the figure below

 3. Install the pydevd-pycharm software package (I use the pip installation method 1, the method is shown in the figure below);

Method 1: The method of installing the pydevd-pycharm package with pip on the server side (the next steps are based on this method!!!)

Method 2: Add the pydevd-pycharm package from the installation directory . Method 2 adds the following code segment at the head of the debugger. (Method 1 is recommended)

#=======this code added==================================
import sys
# <PyCharm directory>为本地计算机下的pycharm路径
sys.path.append("<PyCharm directory>/debug-egg/pydevd-pycharm.egg") # 如采用pip方法安装pydev-pycahrm软件包,则不需要这行代码
import pydevd_pycharm
# 本机cmd命令窗口通过ipconfig指令查询本机ipv4地址
# 端口号可以随便改,可用即可。端口范围一般用到的是1到65535,其中0一般不使用。
pydevd_pycharm.settrace('本机ipv4地址', port=端口号, stdoutToServer=True,
                        stderrToServer=True)
#========================================================

Supplementary content: pip mirror source configuration instructions (can speed up the speed of pip installation)

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com

4. Modify the source code file ( in fact, add a few lines of code at the head of the debugger, see the figure below)

Note: If you choose to add the pydevd-pycharm package from the installation directory, you need to add the line sys.path.append("<PyCharm directory>/debug-egg/pydevd-pycharm.egg") ! ! I installed the pydevd-pycharm package with pip, so there is no need to add this line of program (see the picture below)~~

5. Successful remote debugging code

Additional Notes: Possible problems. .

Close Pycharm, delete the .idea folder in the project directory and restart, delete and reconfigure remote deployment, delete and re-add the python interpreter, which can solve most problems! ! !

报错1:pydev debugger: warning: trying to add breakpoint to file that does not exist: /tmp/xxx

Solution: First, ensure that the server can be connected to during deployment, and that the mapping path is correct. Check whether the path mapping of each step is correct.

Method 1: Delete and re-add the remote python interpreter, this will solve the problem with a high probability! ! (It is best to close Pycharm, delete the .idea folder in the project directory and restart, and then delete and add the python interpreter, the previous wrong operation may be recorded in the control information of the project)

Method 2: Click Pycharm's File>>Invalidate Caches / Restart..., and then select Invalidate and Restart. This operation clears the cached information in the project.

Method 3: Run->View Breakpoint, just uncheck the displayed breakpoint, and you can see that the displayed file path is the same. You can also delete all breakpoints, you can click the red origin as shown in the figure below to operate.

Error 2: Python cannot find the file after pycharm connects to the remote server

Solution:

(2 messages) Solution - Python can't find the file after pycharm connects to the remote server

Error 3: Summary of several reasons why Pycharm cannot debug (step-by-step debugging), and cannot hit a breakpoint and run directly to the end!

Solution: (2 messages) Summary of several reasons why Pycharm cannot debug (step-by-step debugging), and cannot hit a breakpoint and run directly to the end! _Python fan's blog-CSDN blog_pycharm debug failure

Error 4: Run 'pytest in XXX.py' appears when right-clicking the program in Pycharm to return to normal mode

Solution: (2 messages) When you right-click to run the program in Pycharm, Run 'pytest in XXX.py' appears to return to normal mode_qq_40819945's blog-CSDN blog_pytest right-click and change back

Guess you like

Origin blog.csdn.net/xuanhaolaile/article/details/128293254