0

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

{
// 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"
    }
]

}

4

2 回答 2

1

VSCODE 对我来说工作正常

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

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

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

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

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

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

虽然是一个美好的结局,但我可以预见未来的不稳定版本会出现一个精彩的 VSCODE 甚至更好的 Python 扩展

于 2018-10-12T18:48:26.843 回答
0

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

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

当你尝试时会发生什么

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无法决定输出哪个终端的可能性,但同时你只能在调试时得到这个......

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

于 2018-09-23T02:36:04.237 回答