Post

PyCharm专业版远程连接

PyCharm 远程连接有2种方式

1.在远程linux上安装IDE,本地通过JetBrains Gateway控制,原理类似 VSCode,没有本地文件夹的概念

2.打开本地项目后,PyCharm配置连接远程主机,原理是将远程文件夹映射到本地,将修改的本地文件实时同步到远程服务器上

两种方式占用内存对比如下图,方式1 占内存1G,方式2 占2G,方式1 因为避免了本地和远程文件夹的映射减少了资源占用:

240723memory

PyCharm 远程的缓存路径

C:\Users\user\AppData\Local\JetBrains\PyCharm2024.1\remote_sources

PyCharm 远程连接的原理

先把文件同步到本地,在本地执行时,会自动执行服务器上相应的文件,并使用配置好的远程解释器

PyCharm vs VSCode

PyCharm 终端能同时远程连接多台服务器不需要打开多个页面,一个页面中就可以在多个远程主机SFTP和多个解释器(对应多个命令行)之间自由切换,分离本地和远程并手动上传来方便代码回退

正因为此分离,导致资源占用翻倍以及文件传输的过程中出现各种问题(如rsync)

使用心得

1.把本地文件夹作为一个临时仓库,配置好不同服务器的解释器和主机文件夹后,如果需要调试,把相应文件下载到本地文件夹调试,同时会自动上传本地改动;如果不需要调试只是修改内容,在中间页面修改后点击上传到远程文件,不涉及到本地文件,可从远程下载到本地

2.本地调试时,必须保证本地文件在 配置解释器时映射的远程路径有对应的文件,因为解释器不像远程主机一样,配置后不能随意修改映射路径,而是根据需要重新配置解释器

3.更换新的解释器后,如果勾选自动上传文件,会将本地本地文件夹的文件全部上传到设置的解释器远程路径。这控制着文件的实时上传。目前还不知道 工具-部署-自动上传 控制的是什么

PyCharm 远程项目 Debug

配置好 PyCharm 远程连接后,无需下载所有远程项目文件到本地,只需下载某个启动的Python脚本到本地。点击右上角的 更多操作 - 配置 -编辑,左边栏选中待调试的Python脚本,运行 选择 module(针对相对路径,如 qanything_kernel.qanything_server.sanic_api)或 script,脚本形参无需引号或逗号,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--host
0.0.0.0
--port
7665
--model_size
7B
--use_cpu
--use_openai_api
--openai_api_base
http://172.27.33.63:1234/v1
--openai_api_key
empty
--openai_api_model_name
/data/jiangyy/qwen2-0.5b

补充:VScode 远程项目 Debug

Variables ReferenceLaunch configurations插件配置

1. Python

安装 Python Debugger,VSCode 左边栏 运行和调试 - 创建launch.json文件 - 选择调试器Python Debugger,配置launch.json 的内容如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Python",
            "type": "debugpy",
            "request": "launch", 
            // "program": "/data/jiangyy/inconv/qanything_kernel/qanything_server/sanic_api.py",
            "module": "qanything_kernel.qanything_server.sanic_api",
            "console": "integratedTerminal",
            "args": ["--host", "0.0.0.0", 
            "--port", "7665", 
            "--model_size", "7B", 
            "--use_cpu", 
            "--use_openai_api",
            "--openai_api_base", "http://172.27.33.63:1234/v1",
            "--openai_api_key", "empty",
            "--openai_api_model_name", "/data/jiangyy/qwen2-0.5b"
            ],
            // "env": {
            //     "CONDA_PREFIX": "${env:CONDA_PREFIX}"
            // }
        },
    ]
}

2. C++

VSCode 文件夹下 .vscode 包含:

c_cpp_properties.json:cpp 标准及版本信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\NonSystem\\mingw64\\bin\\gcc.exe",
            "intelliSenseMode": "windows-gcc-x64",
            "cppStandard": "c++11",
            "cStandard": "c11"
        }
    ],
    "version": 4
}

launch.json:运行和调试中使用的命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\NonSystem\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

settings.json:工作区相关设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
    "files.associations": {
        "array": "cpp",
        "atomic": "cpp",
        "bit": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "compare": "cpp",
        "concepts": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "string": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "random": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "numbers": "cpp",
        "ostream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp"
    }
}

tasks.json:保存相关任务,可以在launch.json 中调用它们,例如C语言运行调试运行之前需要编译,可以将编译部分写成一个单独的任务,作为调试运行任务的前置任务调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\NonSystem\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

参考

PyCharm配置连接远程主机篇一PyCharm配置连接远程主机篇二

PyCharm 使用矩池云机器教程

趋动云PyCharm连接开发环境

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.