[PYTHON] Try using PyCharm's remote debugging feature

One of the selling features of PyCharm, the Python IDE, is the remote debugging feature. It's a very convenient function, but there weren't many pages that explained how to use it in Japanese, so I'd like to explain how to set it up and how to use it conveniently.

Need for remote debugging

Most cases, such as simple syntax mistakes and bugs that occur every time, can be resolved by local debugging. However, environment-dependent bugs and bugs caused by specific user data are difficult to find with local debugging. Also, I think there are many cases where a virtual environment is prepared locally and executed there. Remote debugging is still effective in that case.

There are two types of functions called remote debugging, so I will explain each of them.

Python Debug Server method

It is a format that starts the debug server locally and accesses it remotely.

Remote preparation

First, prepare the remote environment. There is an archive for debugging in the directory where you installed PyCharm locally, so copy it to a suitable location on the remote server.

scp /Applications/PyCharm.app/Contents/debug-eggs/pycharm-debug.egg user@host:/path/to/pycharm-debug.egg

Pass the above file path on the remote server. Add it to the environment variable "PYTHONPATH" or programmatically add the path to sys.path.

Example: When adding to sys.path

import sys
sys.path.append('/path/to/pycharm-debug.egg')

Local preparation

Next, add a setting to call the local server.

import pydevd
pydevd.settrace(192.168.1.1, port=12345, stdoutToServer=True, stderrToServer=True)

The first argument of settrace is the local hostname (if the remote and local are on different networks, they need to be able to access each other). The second argument is the port number that listens locally.

The preparation is complete.

Debug execution

Then start the server that listens locally. In PyCharm's "Run / Debug Configurations" settings, select "Python Remote Debug" from the + button in the upper left.

スクリーンショット 2016-12-19 20.16.58.png

"Local host name" is the local host name set in the first argument of settrace earlier. For "Port", enter the port number specified in the second argument. Press OK, set a breakpoint, and then click the debug button to go into standby locally.

The remote server just starts normally with runserver etc. Now, when you access the remote server with a browser etc., you can stop at the breakpoint and perform step execution etc. from the local PyCharm.

remote interpreter method

This is a method of connecting with ssh and using a remote interpreter. It may be used infrequently. In addition, this method seems to be available only in the paid Professional Edition.

Enable "SSH Remote Run plugin" from the PyCharm settings screen. スクリーンショット 2016-12-19 12.17.27.png

Then "Settings> Project: server> Project Interpreter" Click the gear mark next to "Project Interpreter" and select add Remote.

Select SSH Credentials and enter the information to log in with SSH. スクリーンショット 2016-12-19 16.02.34.png

Click the button next to path mappings to add the local application directory and the remote application directory.

This will add the remote server interpreter to the interpreter list so you can select and run it locally.

Finally

Since the Python Debug Server method executes pydev, I think it's a good idea to make it easy to switch the ON / OFF setting and turn it ON only when necessary. It's a hassle to set up, but it's a very useful feature, so I highly recommend it.

Recommended Posts

Try using PyCharm's remote debugging feature
Try using Django's template feature
Try using Pelican's draft feature
Try using Tkinter
Try using docker-py
Try using PDFMiner
Try using geopandas
Try using Selenium
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Try using virtualenv (virtualenvwrapper)
[Azure] Try using Azure Functions
Try using virtualenv now
Try using W & B
Try using Django templates.html
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
Try using Tweepy [Python2.7]
Try using Pytorch's collate_fn
Try using PythonTex with Texpad.
[Python] Try using Tkinter's canvas
Try using Jupyter's Docker image
Try using scikit-learn (1) --K-means clustering
Try function optimization using Hyperopt
Try using Azure Logic Apps
Feature detection using opencv (corner detection)
[Kaggle] Try using xg boost
Try using the Twitter API
Try using OpenCV on Windows
Try using Jupyter Notebook dynamically
Try using AWS SageMaker Studio
Try tweeting automatically using Selenium.
Try using SQLAlchemy + MySQL (Part 1)
Try using the Twitter API
Try using SQLAlchemy + MySQL (Part 2)
Try using the PeeringDB 2.0 API
Try using pytest-Overview and Samples-
Organized feature selection using sklearn
Remote debugging with Visual Studio 2017
Try using folium with anaconda