Remote debugging using pycharm

Use the professional version of pycharm, which has the ssh interpreter function in the interpreter settings;

Generally, remote code cannot be debugged locally, and mechanical scp file transfer greatly affects work efficiency. PyCharm Pro supports remote run, debug, and other visual functions.

  • Operating system: local MacOS, remote Linux (all three local operating systems are supported, remote Linux is relatively stable)
  • IDE: Latest version PyCharm Pro (community version not supported)
  • python virtual environment: Anaconda, pip, virtualenv

1. Environment configuration on the remote server

When using local pycharm and pycharm on the remote server,
The following information on the server needs to be obtained;

  1. For the project path on the server, the data set needs to be prepared in advance and placed on the server;
  2. When debugging remotely, you need to use the environment path in the server and specify it under the python path in the virtual environment;
  3. Server user name, ip address, port port;

1.1 Server information

Obtain the following information from the server;

1. project path: pwd
/home/yons/Documents/python_proj/02_patch-mix_contrastive_learning/patch-mix_contrastive_learning-main

2. conda python environments path:
/home/yons/anaconda3/envs/torch2.1.0/bin/python3.8

3. Username: whoami
yons
4.ip, ifconfig
inet 192.168.xx.xx
5. port: use default
 twenty two

1.2 Configuration of remote environment

On the server, there are three things to check,

1.2.1 ufw has been enabled

sudo ufw enable

Check status:
To verify that the SSH rules have been added and the firewall is active, run

sudo ufw status

1.2.2. Allow communication on port 22

·Note, try not to use the default port,

Because hackers know the role of commonly used default ports, they will use port scanning tools to determine the default ports under each network segment.
Then attack the server;
My personal server No. 11.18 was attacked, and after the attack,
As soon as you access the Internet, all the cores of the CPU will be fully occupied, and the computing resources are estimated to be occupied.
So modify the port corresponding to your application service,
For example, if you change other ports, such as changing it to 4000, hackers will not be able to guess what protocol it is and what application it is used for after scanning;

Port 22 is used because the default port in configuring pycharm is 22;
Allow SSH (port 22):
Now that UFW is enabled, SSH traffic on the default port 22 should be allowed. Use the following command:

sudo ufw allow 22/tcp

1.2.3. The ssh service has been started

Check SSH service status:
Make sure the SSH service is running on the remote server. You can check its status using the following command

sudo systemctl status ssh

If the service is not running, start it using the following command:

sudo systemctl start ssh
  • If: unit ssh.service could not be found
    To list all service units with “ssh” in their name, use the following command:
systemctl list-units | grep ssh

If there is no output,
To determine whether SSH is installed, you can use the package management tool specific to your Linux distribution.

dpkg -l | grep openssh-server

If the SSH server is not installed, you should install it using your package manager.
For example, on Ubuntu you can install it using:

sudo apt update
sudo apt install openssh-server

Make sure that the SSH service is indeed installed and running properly on your Ubuntu 20.04 system.

sudo systemctl status ssh.service

If the service is running, you should see its status and be active (running).

When using systemctl with a service name or alias, it is important to use the full service name, in this case ssh.service . Using aliases may not work as expected.

sudo systemctl start ssh.service

1.3 Create or open a project

First create or open a project file on the remote server,

Note that the project file will be synchronized with the local project file later.

You can use the previous Python virtual environment or create a new one yourself.

Here you need to record the path of the project and the python path of conda. Under Linux systems, the virtual environment of anaconda will generally be in the current user directory, for example:

/home/USER/.conda/envs/YOUR_CONDA_ENVIRONMENT/bin/pythonX


Virtual environment python path

1.4 Remot Host

Use the one that comes with pycharm
View the file directory information in the remote server,

2. Local remote connection

2.1 pycharm new project

Use PyCharm to open the previous project locally, or create a new project. Interpretertemporarily selects the local one,
The “temporary” here means that in the future, we will replace it with a virtual environment on the server;

  • Use ? to open the Preferences menu and create a new python environment.

2.2 Add new Interpreter

Preference --> Python Interpreter --> Add

2.3 Log in to the server

  • Select SSH Interpreter –> New server configuration

Fill in the IP, Port, and Username of the remote end, click Next, and enter the remote user login password.

  • Fill in the remote python path and the project path for code synchronization, and click Finish.

You can load the remote python environment locally.

2.4 Set file synchronization mapping

Tools --> Deployment --> Configuration

  • Fill in the IP address of the remote end, Type is SFTP mode, project path, and click OK to complete.

  • To synchronize files, you can use shortcut keys or right-click and click Sync to send the local files to the remote folder. Pycharm will also automatically upload the local files.

2.5 Running and debugging remote code

  • After synchronizing the file and running the python code locally, you can see that the file is running in the remote environment.

  • Debugging, break points can also be implemented.

  • After the remote python environment is created for the first time, it can be found in Exist and can be used repeatedly. You only need to modify the mapped folder path each time;
  • Remote debugging may cause problems in multi-threads and multi-processes. When using pytorch code, try to set the worker to 0;
  • The visualization script currently supports OpenCV and Matplotlib visualization.

reference

https://zhuanlan.zhihu.com/p/265152953