01-10 Tuesday PyCharm remote Linux server configuration for endpoint debugging


01-10 Tuesday PyCharm remote Linux server configuration

Time Version Modifier Description
January 10, 2023 14:04:15 V0.1 Song Quanheng New document
February 6, 2023 11:03:45 V0.2 Song Quanheng Add shortcut command The implementation of aliases facilitates switching of virtual environments

Introduction

Using PyCharm, you can debug your application using an interpreter located on another computer (server).

Refer to this blog

Tools menu

?When using PyCharm, I found that there is no tool menu bar in the menu bar. Configure it in the following way.

image-20230111095503907

Configuration process

Deployment configuration

The main goal of this step is to automatically synchronize local program changes to the remote Linux server through PyCharm, and when local files and server files change, updates can be obtained in both directions.

?In Tools menu->Deployment->Configuration, create SFTP connection (Remote File Transfer Protocol), configure mapping and excluded paths

Connection configuration

image-20230111095927791

?The configuration process is as follows:

?In this way, the connection configuration will be normal.

Mapping configuration

Root path configuration

?When configuring the connection, please note that for Linux servers, the root path is “/”. The default is “/root”. My project is intended to be deployed in the path /home/sqh/FastBuild directory, so the root path is / That’s it.

Mapping configuration

Multiple mappings can be added at the same time when the project has multiple sub-projects and are deployed to different locations.

Excluded paths

This section is mainly used for some unnecessary synchronization, such as the .idea directory generated by pycharm, or the log files generated on the server. Content that does not need to be synchronized can be filtered in this way.

image-20230111102041827

Deploy the project to the server

?The diagram of operating PyCharm to deploy the project to the server is as follows:

?Then use XShell to connect to the 34 server and view the uploaded file directory

Configure python interpreter

?In the blog, I reviewed the above two methods for remote debugging, and the author recommends the first one. After briefly browsing the second method, you need to download the pydevd-pycharm software package and modify the source program at the same time. It is more complicated and will not be demonstrated again. If you have any questions, just refer to the blog.

Create python virtual environment

?First create a python virtual environment, mainly implemented using virtualenv.

Download virtualenv software

?Software installation requires the use of the pip tool.

$ pip3 install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple/
Create a virtual environment
# cd /home/sqh/FastBuild
$ virtualenv envname # Create a virtual environment named envname
$ virtualenv -p python2 envname # If multiple python versions are installed, such as py2 and py3, you need to specify which one to use to create the virtual environment
# Note that you need to specify an environment of python3.7 or above as the FastBuild environment.
$ virtualenv -p /usr/local/dros/python/bin/python3 fastbuild
# The following program is used for verification
(fastbuild) root@szfyd-alg02:/home/sqh/FastBuild# /usr/local/dros/python/bin/python3 -V
Python 3.7.14

?The specific implementation is as follows:

(fastbuild) root@node34-a100:/home/sqh/FastBuild# virtualenv fastbuild
created virtual environment CPython3.7.0.final.0-64 in 353ms
  creator CPython3Posix(dest=/home/sqh/FastBuild/fastbuild, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==22.3.1, setuptools==65.6.3, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

root@node34-a100:/home/sqh/FastBuild# cd fastbuild/
root@node34-a100:/home/sqh/FastBuild/fastbuild# ls
binlibpyvenv.cfg

root@node34-a100:/home/sqh/FastBuild/fastbuild# source bin/activate
(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# pip list
Package Version
---------- -------
pip 22.2.2
setuptools 63.4.1
wheel 0.37.1

[notice] A new release of pip available: 22.2.2 -> 22.3.1
[notice] To update, run: pip install --upgrade pip

?You can see that the virtual environment fastbuild was created in /home/sqh/FastBuild:

(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# ll
total 24
drwxr-xr-x 4 root root 4096 Jan 11 02:52 ./
drwxr-xr-x 10 root root 4096 Jan 11 02:52 ../
drwxr-xr-x 2 root root 4096 Jan 11 02:52 bin/
-rw-r--r-- 1 root root 40 Jan 11 02:52 .gitignore
drwxr-xr-x 3 root root 4096 Jan 11 02:52 lib/
-rw-r--r-- 1 root root 245 Jan 11 02:52 pyvenv.cfg

Activate virtual environment
root@node34-a100:/home/sqh/FastBuild/fastbuild# cd /home/sqh/FastBuild/fastbuild
root@node34-a100:/home/sqh/FastBuild/fastbuild# source bin/activate
(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# ll

Virtual environment operation
# View the current python version in the virtual environment
python -V
# View the current pip version in the virtual environment
pip -V
# View the modules installed in the current virtual environment
pip list
# Install modules in virtual environment
pip install module_name
Cancel virtual environment
(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# which python
/home/sqh/FastBuild/fastbuild/bin/python
(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# pip -V
pip 22.2.2 from /home/sqh/FastBuild/fastbuild/lib/python3.7/site-packages/pip (python 3.7)

(fastbuild) root@node34-a100:/home/sqh/FastBuild# deactivate
root@node34-a100:/home/sqh/FastBuild#
root@node34-a100:/home/sqh/FastBuild# which python
/root/anaconda3/bin/python
root@node34-a100:/home/sqh/FastBuild# which pip
/root/anaconda3/bin/pip

Configuring FastBuild dependencies in a virtual environment

?Project dependencies are located in requirements.txt.

?You can see that there is no software in the initially created virtual environment. Perform software installation and download the required dependencies.

Make sure the fastbuild environment is activated.

(fastbuild) root@node34-a100:/home/sqh/FastBuild# pip list
Package Version
---------- -------
pip 22.2.2
setuptools 63.4.1
wheel 0.37.1

[notice] A new release of pip available: 22.2.2 -> 22.3.1
[notice] To update, run: pip install --upgrade pip
(fastbuild) root@node34-a100:/home/sqh/FastBuild# pip install -r requirements.txt
Collecting anyio==3.6.1
  Using cached anyio-3.6.1-py3-none-any.whl (80 kB)
CollectingAPScheduler==3.9.1
  Using cached APScheduler-3.9.1-py2.py3-none-any.whl (59 kB)
Collecting autopep8==1.7.0
  Using cached autopep8-1.7.0-py2.py3-none-any.whl (45 kB)
Collecting bcrypt==4.0.0
  Using cached bcrypt-4.0.0-cp36-abi3-manylinux_2_24_x86_64.whl (594 kB)
Collecting certification==2022.6.15
  Using cached certifi-2022.6.15-py3-none-any.whl (160 kB)
Collecting cffi==1.15.1
  Using cached cffi-1.15.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (427 kB)
Collecting charset-normalizer==2.1.0
  Using cached charset_normalizer-2.1.0-py3-none-any.whl (39 kB)
Collecting click==8.1.3
  Using cached click-8.1.3-py3-none-any.whl (96 kB)
Collecting colorama==0.4.5
  Using cached colorama-0.4.5-py2.py3-none-any.whl (16 kB)
Collecting cryptography==38.0.1
  Using cached cryptography-38.0.1-cp36-abi3-manylinux_2_24_x86_64.whl (4.0 MB)
Collecting distlib==0.3.5
  Using cached distlib-0.3.5-py2.py3-none-any.whl (466 kB)
Collecting fastapi==0.85.0
  Using cached fastapi-0.85.0-py3-none-any.whl (55 kB)
Collecting filelock==3.8.0
  Using cached filelock-3.8.0-py3-none-any.whl (10 kB)
Collecting greenlet==1.1.3
  Using cached greenlet-1.1.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (150 kB)
Collecting h11==0.14.0
  Using cached h11-0.14.0-py3-none-any.whl (58 kB)
Collecting httptools==0.5.0
  Downloading httptools-0.5.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (404 kB)
     40 4.7/404.7 kB 1.7 MB/s eta 0 :00:00
Collectingidna==3.3
  Using cached idna-3.3-py3-none-any.whl (61 kB)
Collecting loguru==0.6.0
  Using cached loguru-0.6.0-py3-none-any.whl (58 kB)
Collecting paramiko==2.11.0
  Using cached paramiko-2.11.0-py2.py3-none-any.whl (212 kB)
Collecting platformdirs==2.5.2
  Using cached platformdirs-2.5.2-py3-none-any.whl (14 kB)
Collecting pycodestyle==2.9.1
  Using cached pycodestyle-2.9.1-py2.py3-none-any.whl (41 kB)
Collecting pycparser==2.21
  Using cached pycparser-2.21-py2.py3-none-any.whl (118 kB)
Collecting pydantic==1.10.2
  Using cached pydantic-1.10.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.8 MB)
Collecting PyNaCl==1.5.0
  Using cached PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (856 kB)
Collecting python-dotenv==0.21.0
  Downloading python_dotenv-0.21.0-py3-none-any.whl (18 kB)
Collecting pytz==2022.4
  Using cached pytz-2022.4-py2.py3-none-any.whl (500 kB)
Collecting pytz-deprecation-shim==0.1.0.post0
  Using cached pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)
Collecting PyYAML==6.0
  Using cached PyYAML-6.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (596 kB)
Collecting requests==2.28.1
  Using cached requests-2.28.1-py3-none-any.whl (62 kB)
Collecting six==1.16.0
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting sniffio==1.3.0
  Using cached sniffio-1.3.0-py3-none-any.whl (10 kB)
Collecting SQLAlchemy==1.4.41
  Using cached SQLAlchemy-1.4.41-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB)
Collecting starlette==0.20.4
  Using cached starlette-0.20.4-py3-none-any.whl (63 kB)
Collecting toml==0.10.2
  Using cached toml-0.10.2-py2.py3-none-any.whl (16 kB)
Collecting typing_extensions==4.3.0
  Using cached typing_extensions-4.3.0-py3-none-any.whl (25 kB)
Collecting tzdata==2022.4
  Using cached tzdata-2022.4-py2.py3-none-any.whl (336 kB)
Collecting tzlocal==4.2
  Using cached tzlocal-4.2-py3-none-any.whl (19 kB)
Collecting urllib3==1.26.11
  Using cached urllib3-1.26.11-py2.py3-none-any.whl (139 kB)
Collecting utils==1.0.1
  Downloading utils-1.0.1-py2.py3-none-any.whl (21 kB)
Collecting uvicorn==0.18.3
  Using cached uvicorn-0.18.3-py3-none-any.whl (57 kB)
Collecting virtualenv==20.16.3
  Using cached virtualenv-20.16.3-py2.py3-none-any.whl (8.8 MB)
Collecting watchfiles==0.18.1
  Downloading watchfiles-0.18.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)
     1. 2/1.2 MB 9.0 MB/s eta 0 :00:00
Collecting websockets==10.4
  Downloading websockets-10.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (106 kB)
     10 6.7/106.7 kB 31.8 MB/s eta 0 :00:00
Collecting win32-setctime==1.1.0
  Using cached win32_setctime-1.1.0-py3-none-any.whl (3.6 kB)
Requirement already satisfied: setuptools>=0.7 in ./fastbuild/lib/python3.7/site-packages (from APScheduler==3.9.1->-r requirements.txt (line 2)) (63.4.1)
Collecting importlib-metadata
  Downloading importlib_metadata-6.0.0-py3-none-any.whl (21 kB)
Collecting backports.zoneinfo
  Using cached backports.zoneinfo-0.2.1-cp37-cp37m-manylinux1_x86_64.whl (70 kB)
Collecting zipp>=0.5
  Downloading zipp-3.11.0-py3-none-any.whl (6.6 kB)
WARNING: The candidate selected for download or install is a yanked version: 'apscheduler' candidate (version 3.9.1 at https://files.pythonhosted.org/packages/e4/9f/c3937d4babe62504b874d4bf2c0d85aa69c7f59fa84cf6050f3b9dc5d83e/APScheduler-3.9.1-py 2. py3-none-any.whl (from https://pypi.org/simple/apscheduler/) (requires-python:>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, ! =3.3.*, !=3.4.*, <4))
Reason for being yanked: Not compatible with Python 2.7
Installing collected packages: pytz, distlib, zipp, win32-setctime, websockets, utils, urllib3, tzdata, typing_extensions, toml, sniffio, six, PyYAML, python-dotenv, pycparser, pycodestyle, platformdirs, loguru, idna, httptools, greenlet, filelock, colorama, charset-normalizer, certifi, bcrypt, backports.zoneinfo, requests, pytz-deprecation-shim, pydantic, importlib-metadata, h11, cffi, autopep8, anyio, watchfiles, virtualenv, tzlocal, starlette, SQLAlchemy, PyNaCl, cryptography, click, uvicorn, paramiko, fastapi, APScheduler
Successfully installed APScheduler-3.9.1 PyNaCl-1.5.0 PyYAML-6.0 SQLAlchemy-1.4.41 anyio-3.6.1 autopep8-1.7.0 backports.zoneinfo-0.2.1 bcrypt-4.0.0 certifi-2022.6.15 cffi-1.15 .1 charset-normalizer-2.1.0 click-8.1.3 colorama-0.4.5 cryptography-38.0.1 distlib-0.3.5 fastapi-0.85.0 filelock-3.8.0 greenlet-1.1.3 h11-0.14.0 httptools -0.5.0 idna-3.3 importlib-metadata-6.0.0 loguru-0.6.0 paramiko-2.11.0 platformdirs-2.5.2 pycodestyle-2.9.1 pycparser-2.21 pydantic-1.10.2 python-dotenv-0.21.0 pytz -2022.4 pytz-deprecation-shim-0.1.0.post0 requests-2.28.1 six-1.16.0 sniffio-1.3.0 starlette-0.20.4 toml-0.10.2 typing_extensions-4.3.0 tzdata-2022.4 tzlocal-4.2 urllib3 -1.26.11 utils-1.0.1 uvicorn-0.18.3 virtualenv-20.16.3 watchfiles-0.18.1 websockets-10.4 win32-setctime-1.1.0 zipp-3.11.0

[notice] A new release of pip available: 22.2.2 -> 22.3.1
[notice] To update, run: pip install --upgrade pip

(fastbuild) root@node34-a100:/home/sqh/FastBuild# pip list
Package Version
--------------------- ----------
anyio 3.6.1
APScheduler 3.9.1
autopep8 1.7.0
backports.zoneinfo 0.2.1
bcrypt 4.0.0
certifi 2022.6.15
cffi 1.15.1
charset-normalizer 2.1.0
click 8.1.3
colorama 0.4.5
cryptography 38.0.1
distlib 0.3.5
...

Configure shell shortcut commands

?Since you need to frequently use fastbuild, switch to the directory, and activate the virtual environment during debugging, you can use aliases to introduce shell commands. For example, we introduce the fb command to automatically switch to the working directory and activate the virtual environment. . The following configuration can be used:

?Modify the configuration file ~/.bashrc and add the alias statement command, as shown below:

 80 # some more ls aliases
 81 alias ll='ls -alF'
 82 alias la='ls -A'
 83 alias l='ls -CF'
 84 alias fb='cd /home/sqh/FastBuild; source fastbuild/bin/activate'

?Then

source ~/.bashrc

?You can see that after connecting to the server, you can directly type the command fb to switch directories and activate the virtual environment.

*** System restart required ***
Last login: Mon Feb 6 02:13:29 2023 from 10.11.12.108
root@node34-a100:~# fb
(fastbuild) root@node34-a100:/home/sqh/FastBuild#

Configure remote python interpreter

Note: Mainly to configure the project interpreter (select the server's python interpreter through SSH)

?Click Next and select the Python interpreter

Note: Check the sync folder. Since the server generates the directory fastbuild virtual environment in the FastBuild directory, you can use the previously excluded path to exclude the directory to prevent the server from synchronizing the directory to the local machine.

It is equivalent to creating a new python interpreter variable in PyCharm. Next, you need to specify the project to use this interpreter

Configure to use Python interpreter

?Modify the python interpreter environment for project debugging

image-20230111112639346

Remote debugging

?Remotely debug the server code and start the project through pycharm.

image-20230111142306429

Summary

?This article describes in detail the use of PyCharm to debug the code of the remote Linux server. By directly debugging the code on the server, differences on Windows (such as path separators) can be resolved, and when using FastBuild for image building, You need to use the docker running environment, which can be developed more conveniently and quickly by running on the real environment.