You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode调试Notebook导入的Python模块时变量无法访问求助

问题描述

我编写了一个utilities.py脚本,用于存放需对数据集定期执行的函数(基于pandas实现),在process.ipynb中通过import utilities as ut导入无问题。但调试时遇到以下问题:

  • 在脚本目标行设置红色断点,点击Notebook单元格左侧的「debug cell」运行代码
  • 代码停在断点处,但变量窗口无内容;调用dir()、locals()无法识别目标变量,输入变量名mgd或print(mgd)报错:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    NameError: name 'mgd' is not defined
    
  • 系统输出提示:
    0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
    0.00s - to python to disable frozen modules.
    0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
    

当前使用的launch.json配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

补充示例:
data_demo.py代码:

import pandas as pd

class City:
    def __init__(self, df=pd.DataFrame()):
        self.df = df

    def transform_city(self):
        print('Transforming city data...')
        df = self.df.T

        return df

Notebook代码:

import pandas as pd
import data_demo as dd

data = {
    'Name': ['John', 'Anna', 'Peter', 'Linda'],
    'Age': [28, 34, 29, 32],
    'City': ['New York', 'Paris', 'Berlin', 'London']
}

# Create DataFrame
df = pd.DataFrame(data)

# Initialize the City class with the DataFrame
city_instance = dd.City(df)

# Now, call the transform_city method
transformed_city = city_instance.transform_city()

# You can put a breakpoint on the line below in VS Code
print(transformed_city)

在df = self.df.T行设置断点,调试控制台输入df报错同样的NameError。我希望不用print语句就能查看变量内容,求解决办法。


解决方案

1. 替换为Notebook专用调试配置

当前launch.json是针对普通Python文件的配置,调试Jupyter Notebook需要添加专属配置项。修改为:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Debug Cell",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "purpose": ["debug-cell"],
            "console": "integratedTerminal",
            "pythonArgs": ["-Xfrozen_modules=off"]
        }
    ]
}
  • purpose: ["debug-cell"]:明确配置用于Notebook单元格调试,同步上下文
  • pythonArgs: ["-Xfrozen_modules=off"]:解决开头的冻结模块提示,避免断点失效

2. 调整断点位置与变量访问逻辑

  • 确保断点打在已执行的代码行:比如示例中df = self.df.T这行,断点停在此处时df还未完成赋值,自然无法访问。可将断点移到return df行,此时df已创建,调试控制台输入df即可正常访问。
  • 若需查看类实例的属性,直接输入self.df即可访问,无需等待局部变量赋值。

3. 禁用文件验证(可选)

若仍有文件验证报错,添加环境变量到配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Debug Cell",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "purpose": ["debug-cell"],
            "console": "integratedTerminal",
            "pythonArgs": ["-Xfrozen_modules=off"],
            "env": {
                "PYDEVD_DISABLE_FILE_VALIDATION": "1"
            }
        }
    ]
}

4. 更新VS Code插件

确保Python和Jupyter插件为最新版本,旧版本可能存在调试上下文同步的bug。


内容的提问来源于stack exchange,提问作者elksie5000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 19:43:09