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了:

相关推荐
tryCbest2 小时前
PyCharm有利于开发的常用设置
python·pycharm
学嵌入式的小杨同学3 小时前
STM32 进阶封神之路(二十五):ESP8266 深度解析 —— 从 WiFi 通信原理到 AT 指令开发(底层逻辑 + 实战基础)
c++·vscode·stm32·单片机·嵌入式硬件·mcu·智能硬件
专注VB编程开发20年3 小时前
vscode插件开发/ 编辑器 事件大全
ide·vscode·编辑器
FatHonor3 小时前
【golang学习之旅】使用VScode安装配置Go开发环境
vscode·学习·golang
不剪发的Tony老师4 小时前
Spyder:一款面向数据科学的Python集成开发环境
ide·python
少司府4 小时前
C++基础入门:第一个C++程序
java·c语言·开发语言·c++·ide
栗子甜酒4 小时前
统信系统下载VScode+Node
ide·vscode·编辑器
sjmaysee6 小时前
vscode配置django环境并创建django项目(全图文操作)
vscode·django·sqlite
慕诗客7 小时前
VSCODE+EIDE编译和下载单片机程序
ide·vscode·单片机