[Linux] Vscode remotely connects to the server container to debug python [Debug]

I have previously written an article about connecting to a server remotely:

Linux remote development configuration [Vscode] and [Pycharm] https://blog.csdn.net/weixin_42569673/article/details/111481095 icon-default.png?t=LA46https://blog.csdn.net/weixin_42569673/article/details/111481095 We can achieve this through vscode Modify remote server files and write code in real time on the local side. Using the debugging function of vscode can also facilitate debugging on the local side. However, some servers use lxc containers to divide everyone's account and operation space. This creates a path mismatch.

The reason is that the default working directory of vscode will be under /home/user, and the root directory of the personal account built through the container is its own space, and there is no path such as /home/user. So we modified the debug configuration file of vscode as follows:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "internalConsole",
            "cwd": "${fileDirname}"  // 设置相对路径,在debug时可以切换到当前文件所在的目录
         },
    
    
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": XXXXX,  //这个端口随便设置
            "host": "XXX.XX.X.XXX",   //这是远程服务器的ip
            // "pathMappings": [
            //     {
            //         "localRoot": "${workspaceFolder}",
            //         "remoteRoot": "."
            //     }
            // ]
        }
    ]
}

This operation makes the working path search based on the folder where the current Debug file is located, and the path will be correct. At this point, you can debug the python code in the remote server container locally.

Guess you like

Origin blog.csdn.net/weixin_42569673/article/details/121230243