Remote debugging of Python using Pycharm

Source: Internet
Author: User

Background description

Sometimes python application code in the local development environment to run very normal, but put on the line after the strange exception, after repeated troubleshooting or can not find the cause of the problem, so it is thought, if the server environment in the single-step tracking debugging is good.

However, installing an IDE on a server system is certainly unrealistic; remote to the server through SSH, although the use pdb of debugging is feasible, but the operation is more cumbersome, but also not intuitive.

So, can you connect the IDE in the development environment to the server environment and implement the programs that run in the IDE Debug server environment that leverages the development environment? The answer is yes, this is remote Debug.

How Remote Debugging Works

The function of remote debugging is supported in large Ides such as Eclipse, IntelliJ idea, and the principle of implementation is basically the same, which is illustrated by Pycharm.

In the remote debugging mode, the Pycharm (IDE) plays the role of the server side, while the application that runs on the remote computer acts as the client. Because of this, in the remote debugging, you need to set the port in the local development environment and start ide,ide will start listening to the configured port, waiting for the client connection request; How does the application on the remote computer establish a communication connection with the IDE?

For the remote debugging feature, Pycharm provides a pydevd module that exists in pycharm-debug.egg the form of the Pycharm installation path. After the remote computer installs the library file, it can then invoke the pydevd.settrace method, which specifies the IP address and the listening port number of the machine where the IDE is located, to establish a connection to the IDE, and once the connection is established, you can step through the remote computer's programs in the IDE.

Configuration method for remote debugging 1, installing on a remote computer pydevdModule

First, the files are found in the Pycharm installation path of the local development environment pycharm-debug.egg (if the remote computer is running Python3, it is required pycharm-debug-py3k.egg );

Then, pycharm-debug.egg copy the files to the remote computer and add them to the reference path on the remote computer in pycharm-debug.egg several ways:

    • easy_install pycharm-debug.eggInstall with command (PIP command cannot be installed, only use Easy_install)
    • To be pycharm-debug.egg added to PYTHONPATH or sys.path :import sys; sys.path.append(‘/home/leo/app-dependancies/pycharm-debug.egg‘)
    • Unzip to pycharm-debug.egg pydev Copy the folder to the remote application directory

Finally, on the remote computer's python command line input import pydevd , if there is no error, the pydevd module installation is successful.

2, in the local development environment Pycharm in the monitoring configuration

The configuration instructions in Pycharm are as follows:

    • "Run", "Edit configurations"
    • "Add New Configuration", "Python Remote Debug"
    • Fill in Local host name and Port , which refers to the local Local host name development environment of the IP address, and Port then fill in a random more than 10000; it is important to note that because the remote computer needs to be connected to the local development environment, the local IP address should be guaranteed to be accessible remotely
    • "Apply" and "OK"
3. Configuring mapping Mapping 4 in the pycharm of the local development environment, inserting code in the application of the remote computer

Insert the following code into the application of the remote computer.

import pydevdpydevd.settrace(‘100.84.48.156‘, port=31235, stdoutToServer=True, stderrToServer=True)

Where the IP address and port number are consistent with the listening configuration in Pycharm.

5. Start in Pycharm Debug Server

"Run", "Debug ...", select the remote debugging configuration item that you just created, and the Debug Console following information is displayed:

for process connection...Use the following code to connect to the debugger:import pydevdpydevd.settrace(‘100.84.48.156‘, port=31235, stdoutToServer=True, stderrToServer=True)

This indicates that it Debug Server has been started and is in a listening state.

6. Start the application on the remote computer

When you start an application on a remote computer, a pydevd.settrace communication connection is established with the Pycharm in the local development environment when the statement is executed, and you can then step through the local IDE.

It is important to note that the local development environment must ensure that the IP address and port number are accessible from the remote computer, otherwise the connection cannot be established.

$ telnet 100.84.48.156 31235Trying 100.84.48.156...telnet: Unable to connect to remote host: Connection refused$ python devicedectector.pyCould not connect to 100.84.48.156: 31236Traceback (most recent call last):  File "/usr/local/lib/python2.7/dist-packages/pycharm-debug.egg/pydevd_comm.py", line 478, in StartClient s.connect((host, port)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args)error: [Errno 111] Connection refused

Remote debugging of Python using Pycharm

Related Article

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.