VScode调试能实现和Pycharm一样的tensor shape,ssh远程连接linux服务器版

本文针对的是本地安装vscode远程连接服务器上面的项目,首先明确你要改的是远端文件,也就是服务器上的文件,因为真正工作的调试器通常跑在服务器上的 ~/.vscode-server/extensions/... 目录。

找一下文件位置,执行:

bash 复制代码
find ~/.vscode-server ~/.cursor-server -path '*/ms-python.debugpy-*/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_xml.py' 2>/dev/null

找到文件路径:/disk2/***(你的用户名)/.vscode-server/extensions/ms-python.debugpy-***-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_xml.py,先备份一下代码,执行备份命令:

bash 复制代码
cp /disk2/***/.vscode-server/extensions/ms-python.debugpy-***-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_xml.py /disk2/***/.vscode-server/extensions/ms-python.debugpy-***-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_xml.py.bak

然后确认一下备份成功:

bash 复制代码
ls -l /disk2/wxr/.vscode-server/extensions/ms-python.debugpy-2025.18.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_xml.py*

你应该能看到两个文件:

  • pydevd_xml.py
  • pydevd_xml.py.bak

打开pydevd_xml.py文件,找到def get_variable_details函数,替换成下面的版本:

python 复制代码
def get_variable_details(val, evaluate_full_value=True, to_string=None, context: Optional[str] = None):
    """
    :param context:
        This is the context in which the variable is being requested. Valid values:
            "watch",
            "repl",
            "hover",
            "clipboard"
    """
    try:
        is_exception_on_eval = val.__class__ == ExceptionOnEvaluate
    except:
        is_exception_on_eval = False

    if is_exception_on_eval:
        v = val.result
    else:
        v = val

    _type, type_name, resolver = get_type(v)
    type_qualifier = getattr(_type, "__module__", "")

    if not evaluate_full_value:
        value = DEFAULT_VALUE
    else:
        try:
            shape_info = ""
            try:
                # PyTorch Tensor
                if hasattr_checked(v, "shape") and hasattr_checked(v, "dtype") and "torch" in type_qualifier:
                    try:
                        shape = tuple(v.shape)
                    except:
                        shape = v.shape
                    dtype = str(v.dtype)
                    shape_info = f"{{Tensor shape={shape}, dtype={dtype}}} "

                # NumPy ndarray
                elif hasattr_checked(v, "shape") and hasattr_checked(v, "dtype") and "numpy" in type_qualifier:
                    try:
                        shape = tuple(v.shape)
                    except:
                        shape = v.shape
                    dtype = str(v.dtype)
                    shape_info = f"{{ndarray shape={shape}, dtype={dtype}}} "

                # Pandas DataFrame
                elif "pandas.core.frame" in type_qualifier and hasattr_checked(v, "shape"):
                    try:
                        shape = tuple(v.shape)
                    except:
                        shape = v.shape
                    shape_info = f"{{DataFrame shape={shape}}} "

                # Pandas Series
                elif "pandas.core.series" in type_qualifier and hasattr_checked(v, "shape"):
                    try:
                        shape = tuple(v.shape)
                    except:
                        shape = v.shape
                    dtype = str(v.dtype) if hasattr_checked(v, "dtype") else "unknown"
                    shape_info = f"{{Series shape={shape}, dtype={dtype}}} "

                # Other objects with shape
                elif hasattr_checked(v, "shape"):
                    try:
                        shape = tuple(v.shape)
                    except:
                        shape = v.shape
                    shape_info = f"{{shape={shape}}} "

                # Sized containers
                elif hasattr_checked(v, "__len__"):
                    try:
                        length = len(v)
                        if type_name == "str":
                            shape_info = "{str} "
                        else:
                            shape_info = f"{{{type_name}: {length}}} "
                    except:
                        pass
            except:
                pass

            str_from_provider = _str_from_providers(v, _type, type_name, context)
            if str_from_provider is not None:
                value = shape_info + str_from_provider

            elif to_string is not None:
                value = shape_info + to_string(v)

            elif hasattr_checked(v, "__class__"):
                if v.__class__ == frame_type:
                    value = pydevd_resolver.frameResolver.get_frame_name(v)

                elif v.__class__ in (list, tuple):
                    if len(v) > 300:
                        value = "%s: %s" % (str(v.__class__), "<Too big to print. Len: %s>" % (len(v),))
                    else:
                        value = "%s: %s" % (str(v.__class__), v)
                else:
                    try:
                        cName = str(v.__class__)
                        if cName.find(".") != -1:
                            cName = cName.split(".")[-1]

                        elif cName.find("'") != -1:
                            cName = cName[cName.index("'") + 1:]

                        if cName.endswith("'>"):
                            cName = cName[:-2]
                    except:
                        cName = str(v.__class__)

                    value = "%s: %s" % (cName, v)
                    if shape_info:
                        value = shape_info + value
            else:
                value = shape_info + str(v)

        except:
            try:
                value = repr(v)
            except:
                value = "Unable to get repr for %s" % v.__class__

    try:
        if value.__class__ == bytes:
            value = value.decode("utf-8", "replace")
    except TypeError:
        pass

    return type_name, type_qualifier, is_exception_on_eval, resolver, value

保存之后退出。

最后很重要:重启 VS Code 远程会话!!重启之后打开本地VSCode,按Ctrl + Shift + P输入并执行Developer: Reload Window重新连接一次服务器。

然后就和pycharm一样能看到tensor shape了:

相关推荐
dyxal2 小时前
VS Code 终端疑难杂症排查:为什么 PowerShell 无法启动?
vscode
【ql君】qlexcel2 小时前
Visual Studio Code开发STM32设置头文件宏定义uint32_t报错
vscode·stm32·vs code·头文件宏定义·uint32_t报错·uint8_t报错·uint16_t报错
琉璃榴2 小时前
Visual Studio Code连接远程服务器
服务器·vscode·github
HuDie3404 小时前
agent项目实操笔记
ide
jieyucx4 小时前
Golang 完整安装与 VSCode 开发环境搭建教程
开发语言·vscode·golang
梦魇星虹5 小时前
idea Cannot find declaration to go to
java·ide·intellij-idea
xifangge20255 小时前
【故障排查】IDEA 打开 Java 文件没有运行按钮(Run)?深度解析项目标识与环境配置的 3 大底层坑点
java·ide·intellij-idea
刘延林.7 小时前
Visual Studio Code+PlatformIO + ESP32-S3 + Arduino 框架点亮一个小的led灯-测试
ide·vscode·编辑器
黑客大白8 小时前
IDEA安装教程配置java环境(超详细)_idea配置java,零基础入门到精通,收藏这篇就够了
java·ide·intellij-idea
Dontla9 小时前
VSCode插件Git Graph介绍(Git可视化管理分支、可视化Git)
ide·git·vscode