Python uses PyCharm for remote development and debugging, and pythonpycharm debugging

Source: Internet
Author: User
Tags django server virtual environment

Python uses PyCharm for remote development and debugging, and pythonpycharm debugging

Background description

Sometimes the Python application code runs normally in the local development environment, but after it is put online, there is an inexplicable exception. After repeated troubleshooting, the cause of the problem still cannot be found, so I am thinking, it would be nice to perform one-step tracking and debugging in the server environment.

However, it is unrealistic to install an IDE on the server system. Although it is feasible to remotely connect to the server through SSH and use pdb for debugging, the operation is still complicated and not intuitive enough.

Can I connect the IDE in the development environment to the server environment to debug programs running in the server environment using the IDE in the development environment? The answer is yes. This is Remote Debug ).

Working principle of remote debugging

The functions of remote debugging are supported in Eclipse, IntelliJ IDEA, and other large ides. The implementation principles are basically the same. Here, PyCharm is used to describe the functions.

In remote debugging mode, PyCharm (IDE) plays the role of Server, while applications running on remote computers play the role of Client. For this reason, during remote debugging, you must first set the port in the local development environment and start the IDE. The IDE will start listening to the set port and wait for the client connection request; then how does the application in the remote computer establish a communication connection with the IDE?

For the remote debugging function, PyCharm provides the pydevd module, which exists in the installation path of PyCharm in the form of pycharm-debug.egg. After installing the library file on a remote computer, you can call pydevd. settrace method, which specifies the IP address of the IDE machine and the listening port number, used to establish a connection with the IDE. After the connection is established, you can perform one-step debugging on programs remotely on a remote computer in the IDE.

Use PyCharm for remote development and debugging

Do you often need to develop Python or Web applications on Windows 7 or mac OS X, but do they need to run on linux? We often encounter problems in the development but in the official Linux environment. So how can we ensure the consistency between the development environment and the running environment?

There are usually two solutions. One is to use PyCharm built-in supportVagrantFor this tutorial, see Vagrant Development Environment configuration. However, it is a pity that I have always reported an error when I started the VirtualBox virtual machine during the experiment. The problem has not been solved yet. Readers can try to test it by themselves. The second method is to use the PyCharm remote interpreter with the file synchronization function to locally edit the code-> synchronize to the server-> debug the test program remotely. At present, I chose the second one, although it is more clumsy than the first one.

The functions of remote debugging are supported in Eclipse, IntelliJ IDEA, and other large ides. The implementation principles are basically the same. Here, PyCharm is used to describe the functions.

Remote Server synchronization Configuration

Remote Server IP address 192.168.203.95, enable the ssh service, and install python 2.7. I used a core-python project in PyCharm for demonstration.

First, we need to configure PyCharm to synchronize code through the server, open Tools | Deployment | Configuration

Click "+" on the left to add a deployment configuration. Enter the name and select SFTP as the type.

After confirming, configure the ip address, port, user name, and password of the remote server. Root path is the root directory for file upload. Note that this directory must have the user name and permission to create a file.

Then configure the ing. The local path is your project directory, that is, you need to synchronize the local directory to the server. I fill in the project root directory. Deploy path on server fill in the directory relative to the root path here, the following web path does not need to be checked first

If you still have some files or folders that do not want to be synchronized, you can add them in the "Excluded path" on the third tab page of the configuration dialog box to specify both local and remote.

Another setting is to open Tools | Deployment | Options and tick "Create Empty directories". If the specified folder does not exist, it is automatically created.

Upload and download files

There are several ways to synchronize local and remote files, which are triggered manually and automatically after the files are saved. Here I chose manual, because automatic triggering, for example, affects performance. PyCharm will be stuck and it feels uncomfortable.

The manual Upload method is simple. Select the file or folder to be synchronized, and then select Tools | Deployment | Upload to sftp (this is the Deployment Name Just configured)

The same is true for downloading files. Select Tools | Deployment | Download from sftp.

Compare remote and local files

Sometimes you are not sure that the remote and local versions are exactly the same. You need to compare them. PyCharm provides a comparison view to solve this problem.

Select Tools | Deployment | Browse Remote Host to open the Remote file view. The files in the Remote Host are displayed in the right window.

Select the folder you want to compare, right click-> Sync with Local, open the synchronization comparison window, and use the left and right arrows to synchronize the content.

The above is the comparison between the server and the local, so the local file is compared with the server, first select a file or folder in PyCharm, and then right-click-> Deployment-> Sync with deployed.

PyCharm remote debugging

There are two options for remote debugging in PyCharm:

  1. Use Remote Interpreter
  2. Debug the server using Python

For the sake of simplicity, I will only demonstrate the first method, using the remote interpreter, that is, using the python interpreter installed on the server.

Configure remote Python Interpreter

Select File | Settings, select Project | Project Interpreter, and click the gear on the right side, as shown below:

Click "Add Remote" and enter the ssh configuration of the host.

If SFTP has been configured before, select "Deployment configuration" and select the Template Name. Because I have configured the template above, select the template directly, the Python interpreter is a virtual environment virtualenv. You must create a virtual environment on the server.

Start debugging

After completion, select the remote interpreter as the project interpreter, configure a running instance, and initiate breakpoint debugging. Here I use another django project as an example to describe it. The name is zspace, because a web project is more representative.

Select "Run/Debug Configuration", add a "Django server", and write the Configuration as follows:

Pay attention to the points marked in the figure. You don't need to explain the specific meaning. ^_^

Then you can initiate a breakpoint for debugging just like local debugging. This step is too simple. Remember to modify the source code and synchronize it to the server for the next debugging.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.