【问题标题】:VS code python debugger error with print statements带有打印语句的VS代码python调试器错误
【发布时间】:2018-09-23 01:55:02
【问题描述】:

因此,当我尝试在 VS 代码上调试任何 Python 脚本并且该脚本具有打印或输入语句时,它会崩溃并向我抛出一个“AttributeError”,上面写着“NoneType”对象没有“写入”属性,任何想法为什么会这样?我在谷歌上找不到任何关于它的信息 这是错误中的屏幕截图:Link to the screenshot 这也是我的配置文件:

{
// Use IntelliSense para saber los atributos posibles.
// Mantenga el puntero para ver las descripciones de los existentes atributos 
// Para más información, visite: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File (Integrated Terminal)",
        "type": "python",
        "request": "launch",
        "stopOnEntry": true,
        "program": "${file}",
        "console": "integratedTerminal"
    },
    {
        "name": "Python: Attach",
        "type": "python",
        "request": "attach",
        "port": 5678,
        "host": "localhost"
    },
    {
        "name": "Python: Django",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/manage.py",
        "console": "integratedTerminal",
        "args": [
            "runserver",
            "--noreload",
            "--nothreading"
        ],
        "django": true
    },
    {
        "name": "Python: Flask",
        "type": "python",
        "request": "launch",
        "module": "flask",
        "env": {
            "FLASK_APP": "app.py"
        },
        "args": [
            "run",
            "--no-debugger",
            "--no-reload"
        ],
        "jinja": true
    },
    {
        "name": "Python: Current File (External Terminal)",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "externalTerminal"
    }
]

}

【问题讨论】:

    标签: python python-3.x visual-studio-code vscode-debugger


    【解决方案1】:

    VSCODE 对我来说工作正常

    a) 安装 VSCODE 或升级到 1.28.1 版本 b) 刷新 Python 扩展

    如果您遇到任何问题,例如超时等,请阅读 https://github.com/Microsoft/vscode-python/issues/2410 很认真

    编辑python扩展的settings.json a) 禁用 终端:激活环境 在使用扩展创建的终端中激活 Python 环境。

    b) 启用 终端:在文件目录中执行 在终端执行文件时,是否使用在文件所在目录执行,而不是在当前打开的文件夹中执行。

    c) 移除 pythonW 并将 python 放入 蟒蛇路径 Python 的路径,您可以通过修改此设置以包含完整路径来使用自定义版本的 Python。

    以上所有内容来自 https://github.com/Microsoft/vscode-python/issues/2410

    虽然结局很圆满,但我可以预见未来的不稳定版本 获得精彩的 VSCODE 甚至更好的 Python 扩展

    【讨论】:

      【解决方案2】:

      属性错误通常意味着您使用的任何对象实际上都没有。这可能是因为在您的通话上游或下游发生了一些事情。

      在你的单打印语句的情况下,我唯一能想到的可能是它有一些用双引号来做的事情。双引号会导致这种情况并没有真正的意义,但谁知道呢。

      当你尝试时会发生什么

      print('I will crash!!!')
      

      如果仍然失败,那么我会说也许 vs 正在尝试写入文件、配置、日志、控制台或其他内容,并且遇到了权限问题。

      编辑 在查看您的配置文件之后,我看到您有两个以

      开头
      "name": "Python: Current File ....
      

      所以我重写了您的配置文件,它仍然包含已命名的特定文件及其配置,但我删除了当前文件条目之一并使其成为基本文件。

      {
      // Use IntelliSense para saber los atributos posibles.
      // Mantenga el puntero para ver las descripciones de los existentes atributos 
      // Para más información, visite: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
          {
              "name": "Python: Current File (External Terminal)",
              "type": "python",
              "request": "launch",
              "program": "${file}",
              "console": "externalTerminal"
          },
          {
              "name": "Python: Attach",
              "type": "python",
              "request": "attach",
              "port": 5678,
              "host": "localhost"
          },
          {
              "name": "Python: Django",
              "type": "python",
              "request": "launch",
              "program": "${workspaceFolder}/manage.py",
              "console": "integratedTerminal",
              "args": [
                  "runserver",
                  "--noreload",
                  "--nothreading"
              ],
              "django": true
          },
          {
              "name": "Python: Flask",
              "type": "python",
              "request": "launch",
              "module": "flask",
              "env": {
                  "FLASK_APP": "app.py"
              },
              "args": [
                  "run",
                  "--no-debugger",
                  "--no-reload"
              ],
              "jinja": true
          }
      ]
      }
      

      我已将其设置为使用外部控制台(标准 Windows cmd)。如果你想使用 vs 控制台替换 ​​

      {
          "name": "Python: Current File (External Terminal)",
          "type": "python",
          "request": "launch",
          "program": "${file}",
          "console": "externalTerminal"
      },
      

      {
          "name": "Python: Current File (Integrated Terminal)",
          "type": "python",
          "request": "launch",
          "stopOnEntry": true,
          "program": "${file}",
          "console": "integratedTerminal"
      },
      

      不要忘记先保存旧配置文件的副本。这样,如果 VS 因手动更改此文件而崩溃,您可以随时恢复。

      我正在寻找 VS 也无法决定输出哪个终端的可能性,但同时你只能在调试时得到这个......

      现在我确实在配置中看到了一个没有调试的标志,但它是针对烧瓶应用程序的。

      【讨论】:

      • 它仍然崩溃,关于我如何检查它是否试图写入文件的任何想法?如果我不是打印,例如输入(“我会崩溃!”),那么 AttributeError 会说“Nonetype”对象没有属性“readline”,另外,这只发生在我调试它时
      • 是每个文件都会发生这种情况,还是只有这一个文件会发生这种情况?如果您要使用单独的打印语句从头开始创建新文件,会发生什么。
      • 您的屏幕截图还显示了 exception 下的局部变量中的一些数据,您可以发布这些数据吗?
      • 很容易重现在调试控制台类型 print("jnc") print("jnc") AttributeError("'NoneType' object has no attribute 'write'",) 它发生在我的 Windows 10最新更新的 VSCODE 版本:1.28.0(系统设置)提交:431ef9da3cf88a7e164f9d33bf62695e07c6c2a9 日期:2018-10-05T14:58:53.203Z 电子:2.0.9 Chrome:61.0.3163.100 Node.js:8.9.3 V8.1 架构:6.6 :x64
      • 已提出问题 github.com/Microsoft/vscode/issues/60436 这发生在 1 台 PC 中,但不会发生在另一台 PC 中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 2013-01-04
      • 2020-10-09
      相关资源
      最近更新 更多