PyCharm 2018.2 Help

Remote Debugging with PyCharm

With PyCharm you can debug your application using an interpreter that is located on the other computer? for example, on a web server or dedicated test machine.

PyCharm provides two ways to debug remotely:

  • Through a remote interpreter.

    Case: Use this approach to leverage extended debugging capabilities available on the remote machine.

    Requirements: SSH access from the local machine to the remote server.

  • Using the Python remote debug server configuration.

    Case: Use this approach to integrate the debugging process into the series of running processes on the remote server. This might be helpful when you cannot explicitly run your application for debugging, or when some preparations tasks are required.

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

Before you start

Complete the following preparation tasks:

  1. On the local machine, create a pure Python project, as described in the section Creating Pure Python Project.
  2. Add a Python file to this project (Alt+Insert - Python File).
  3. Add the following code to the Python File:
    import math 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)

Creating a deployment configuration for a remote interpreter

In this example, the machine where you run your application is referenced as local, and the machine with the remote interpreter is referenced as remote.

Configure a remote interpreter

  1. Ensure that you have SSH access to the remote machine.
  2. Add a new remote interpreter to the project as described in Configuring Remote Interpreters via SSH specifying the credentials to connect to the remote machine.
  3. Once you create the remote interpreter for your project, the corresponding deployment configuration is created. To preview it, click Ctrl+Alt+S to open the Settings dialog window on the local machine, then click the Build, Execution, Deployment node and the Deployment node.
    py remote debugging deployment config
  4. You can accept all default settings or alter them, if needed. For this example, let's use a meaningful name for your deployment configuration, for example, "MySFTPConnection".
  5. Ensure that Root path value reflects the path specified in the corresponding settings of the created SSH interpreter (check the Path Interpreter in the Project | Project Interpreter settings/preferences).
    Path mappings verification

Now your deployment configuration is ready.

Deploy your application to a remote host

Next, your application must be deployed to the remote host.

  1. On the Tools menu, point to Deployment node, and then choose Upload to MySFTPConnection:
  2. File Transfer tool window appears. Verify the number of transferred files.
    File Transfer window

Debugging your application

  1. Right-click the editor background and choose the Debug <name> (here Debug 'quadratic_equation').
  2. Review the debugging output. Note that debugging actually takes place on the specified remote server.
    Debugging on the remote server

Remote debugging with the Python remote debug server configuration

You can also enable remote debugging with the dedicated run/debug configuration, namely, Run/Debug Configuration: Python Remote Debug.

Create a run/debug configuration

  1. On the main menu, choose Run| Edit Configuration.... The Run/Debug Configurations Dialog opens. You have to click icons general add on the toolbar, and from the list of available configurations, select Python Remote Debug.
    Adding a Python remote debug configuration
  2. Enter the name of this run/debug configuration - let it be MyRemoteServer. Specify the port number (here 12345) and the Local host name (here 172.20.208.23) of the machine where the IDE is running. These parameters will be used by the remote debug server to access it.
  3. Map the path on the local machine to the path on the remote machine:
    Path mapping
  4. Inspect the Update your script instructions and perform the following changes:
    • Copy pycharm-debug-py3k.egg for Python 3 or pycharm-debug.egg for Python 2 to the project root. These archives reside in the debug-eggs directory under the PyCharm installation. Refer to Requirements, Installation and Launching for more information about the default installation directories. If you have installed PyCharm using Toolbox , the PyCharm directory is located under the following path: C:\Users\<user_name>\AppData\Local\JetBrains\Toolbox\PyCharm\* on Windows and /Users/<user_name>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm/* on Mac.
    • Modify the source code file as follows:
    import math #==============this code added==================================================================: import sys sys.path.append("pycharm-debug-py3k.egg") import pydevd pydevd.settrace('172.20.208.23', 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)

Create a SFTP connection

  1. On the remote machine, create a directory where the file quadratic_equation.py should be uploaded. You can do it in the Terminal window:
    $cd /tmp $mkdir pycharm_project_986
  2. Create a connection profile. On the main menu, choose Tools | Deployment - Configuration.... In the dialog box that opens, click icons general add, and in the Add Server dialog select the connection type (here SFTP) and enter its name (here MySFTPConnection.)
  3. In the Connection tab, specify the SFTP host (address of the remote machine), user name and password for that machine.

    Note that the specified user should have SSH access to the remote host.

  4. Click Mappings tab, and enter the deployment path in server. The server is MySFTPConnection, so click the browse button and select the required folder. Note that the browse button shows the contents of the remote host. Apply changes and close the dialog.

Deploy files to the remote machine

Deploy the two files (copy of pycharm-debug-py3k.egg (or pycharm-debug.egg for Python 2) and quadratic_equation.py) from the local machine to the remote.

  1. On the local machine, in the Project Tool Window, select the *.egg and quadratic_equation.py files, right-click the selection and choose Deployment | Upload to MySFTPConnection.
  2. Inspect the File Transfer dialog window to ensure that the files from the local machine are uploaded to the remote server.

Launch the Debug Server

  1. Choose the created run/debug configuration, and click icons actions startDebugger svg:
    Running a debug configuration
  2. Ensure that the Debug Tool Window shows the Waiting for process connection.. message. This message will be shown until you launch your script on the remote machine, and this script will connect to the Debug Server.

Execute the Python file on the remote machine

  1. On the remote machine, navigate to the tmp/pycharm_project_986 directory.
  2. Launch the quadratic_equation.py file on the remote host. To do that, in the Terminal window, enter the following command:

For Python 3

$python3 quadratic_equation.py

For Python 2

$python quadratic_equation.py

The most helpful aspect of this debugging method is that you can run execution the Python file using any of your bash scripts when remote debugging is part of a scheduled task or when you need to execute some preparation steps before running the Python script. If that's the case, add the following lines to the appropriate place of your bash script:

For Python 3

cd /tmp/pycharm_project_986 python3 quadratic_equation.py

For Python 2

cd /tmp/pycharm_project_986 python quadratic_equation.py

Debug your application

  • On your local machine, switch to the Debug Tool Window. It should show the connection to the pydev debugger.

    py remote debug result

    Your code is actually executed on the remote host, but debugged on the local machine.

Summary

In order to debug with a remote interpreter, you have to start your program through PyCharm, which is not always possible. On the other hand, when using the Debug Server, you can connect to a running process.

Compare the two approaches.

In the first case, we

In the second case, we

Last modified: 20 July 2018