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

相关推荐
洛兮银儿2 小时前
pycharm选中同一个词的标记颜色的修改
ide·python·pycharm
2601_962381582 小时前
VSCode如何配置LlamaIndex RAG(检索增强生成)应用开发环境
vscode·开发环境·rag·llamaindex·检索增强生成
郝学胜_神的一滴2 小时前
Effective Python 条款 6:善用拆包告别恼人的下标索引
python·pycharm
2601_962382433 小时前
怎么使用pycharm开发图形化界面
pycharm·教程·开发·图形化界面·pyqt5
北漂燕郊杨哥4 小时前
VS Code 安装微信小程序 MCP 介绍
vscode·微信小程序·小程序·mcp
Sherotree12 小时前
Google Antigravity——Agent 被提到主界面
前端·ide·后端·edge浏览器
YZJenny14 小时前
在服务器上安装使用code-server(Web 版 VS Code)
ide·vscode·编辑器
salestina1 天前
vscode迁移扩展目录
ide·vscode·编辑器
sunoo-2292 天前
【Linux 系统编程】学习第七天:线程属性 | 互斥锁 | 死锁 | 信号量 核心知识点 + 踩坑实战
linux·c语言·笔记·vscode·学习
铁手飞鹰2 天前
VSCode Python .embed 嵌入式环境黄色波浪线问题
ide·vscode·python