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.
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.
It is a format that starts the debug server locally and accesses it remotely.
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')
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.
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.
"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.
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.
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.
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.
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