【Linux】Vscode远程连接服务器容器调试python【Debug】

我之前写过一个远程连接服务器的文章:

Linux远程开发配置【Vscode】与【Pycharm】https://blog.csdn.net/weixin_42569673/article/details/111481095icon-default.png?t=LA46https://blog.csdn.net/weixin_42569673/article/details/111481095通过vscode我们可以实现在本地端实时修改远程服务器文件,编写代码,使用vscode的调试功能也可以在本地端方便Debug。但是有的服务器是通过lxc容器来划分每个人的账户和操作空间的。这就造成了路径不匹配。

原因是vscode默认工作目录会找/home/user下,而通过container构建起的个人账户根目录为自己的空间,并没有/home/user等路径。于是我们修改vscode的debug配置文件如下:

{
    // 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": "."
            //     }
            // ]
        }
    ]
}

该操作使工作路径以当前Debug文件所在文件夹为基准进行搜索,路径就正确了。至此可以在本地端调试远程服务器容器中的python代码。

posted @ 2021-11-09 16:23  Jack-Lin  阅读(424)  评论(0编辑  收藏  举报