Super detailed graphics and text teach you how to use PyCharm for remote debugging

Recommended reading: http://pycharm.iswbm.com

This article is a previous article. Some friends have already read it, but it doesn't matter, because this time I am going to introduce how these major debugging tools are debugged and how to choose.

Under normal circumstances, we are developing and debugging on a personal PC is completed, a problem, open about Pycharmthe debugger will soon be able to find the problem.

But sometimes, the operation of the project code depends on the operating environment, and it can only be run on the server where the relevant dependent components are deployed, which directly leads to the inability to debug locally.

For this special scenario, as far as I know, there are two solutions as follows:

  • pdb
  • Remote Debug

This article will talk about the second option, it is 专业版Pycharmonly open function, you need to install the Professional Edition of Pycharm, upgrade cracked concrete steps, your own Google, are not relevant here.

The meaning of remote debugging is that we can use Pycharm's graphical interface to debug code on our PC. It is not much different from local debugging. The original debugging method is still debugging.

The difference is that local debugging does not require prior configuration, as long as your code is ready, you can start debugging at any time. The remote debugging requires a lot of pre-steps, and these setting procedures are also the main content of this article.

1. Create a new project

First, we need to create an empty project in Pycharm. Later we will pull the project code on the server and place it in this project directory. My name here is NOVA, you can define it yourself.

2. Configure Connection Server

Tools -> Deployment -> configuration

add oneServer

  • Name: fill in the IP of your server

  • Type: Set to SFTP

After clicking OK, enter the following interface, you can fill in the information according to my remarks:

  • SFTP host: public network ip
  • Port: the open ssh port of the server
  • Root path: The project code directory you want to debug
  • Username: the user you used to log in to the server
  • Auth type: login type, if you log in with a password, it is Password
  • Password: After choosing a password to log in, enter your login password here, you can choose to save the password.

Please note here, to ensure that your computer can ssh to connect to your server, whether it is key login or password login, if the whitelist restriction is enabled, you must first remove it.

After filling in, switch to the Mappingstab, in the arrow position, fill in\

After the above server information configuration is filled in correctly, clickOK

Next, we have to connect to the remote server. Tools -> Deployment -> Browse Remote Host

3. Download the project code

If the server login information previously filled in is correct, you can now see the remote project code.

Choose to download the remote code locally.

The download is complete.

The current IDE interface should look like this.

4. Download the remote interpreter

Why is this step necessary?

Remote debugging runs on a remote server. In addition to relying on other components, there are also many Python dependencies that we don't have locally.

Go to File -> Settings and press the icon to add a remote interpreter.

Fill in the remote server information as before, so I won’t repeat it.

After clicking OK, the remote interpreter will be downloaded automatically. If your project is relatively large, this time may be longer, please be patient.

5. Add program entry

Because we want to DEBUG locally, you must know the entry program of your project. If this entry program is already included in your project code, please skip this step.

If not, please generate the entry program yourself.

For example, the project on my side runs as a service on the server. And we all know that the entrance to the service is Service文件.cat /usr/lib/systemd/system/openstack-nova-compute.service

[Unit]
Description=OpenStack Nova Compute Server
After=syslog.target network.target libvirtd.service

[Service]
Environment=LIBGUESTFS_ATTACH_METHOD=appliance
Type=notify
NotifyAccess=all
TimeoutStartSec=0
Restart=always
User=nova
ExecStart=/usr/bin/nova-compute

[Install]
WantedBy=multi-user.target

See that ExecStart? That is the entrance of our program. We only need to copy it to our Pycharm and synchronize the file remotely.

6. Settings before commissioning

Turn on automatic code synchronization, so that Pycharm can recognize our code changes and submit them to the remote server for us.

Turn it on Gevent compatible. If it is not turned on, during the debugging process, there may be problems such as failure to debug, or failure to track/view variables.

7. Start debugging code

Right-click on the entry file of your program and select Debug.

If your program entry needs to introduce parameters, this is a common thing, you can configure it here.

Click Save after configuration.

8. Friendly reminder

According to the trial debugging code in the article, the code will be automatically synchronized to the remote end. Do not use it in a production environment. You must use it in a development environment, otherwise you will be responsible for the consequences.

Debugging tools provide programmers with great convenience, but I still hope that you don't rely too much on them. Try to improve your coding ability every time you write code.

Guess you like

Origin blog.csdn.net/weixin_36338224/article/details/109378082