【问题标题】:Python "with" statement causes error in Visual StudioPython“with”语句在 Visual Studio 中导致错误
【发布时间】:2023-04-01 20:24:02
【问题描述】:

我将 Python (3.9) 与 Microsoft Visual Studio Community 2019 结合使用 版本 16.7.7

我尝试使用示例来编写配置文件。 https://tutswiki.com/read-write-config-files-in-python/

Visual Studio 在行尾的关键字“open”、“as”和“:”处向我报告语法错误。将其标记为红色。 “with”和“as”语句也没有突出显示。 如果我强制 Visual Studio 运行,它运行良好。

有谁知道,Visual Studio 报告的问题是什么以及如何解决它。

from configparser import ConfigParser

#Get the configparser object
config_object = ConfigParser()

#Assume we need 2 sections in the config file, let's call them USERINFO and SERVERCONFIG
config_object["USERINFO"] = {
    "admin": "Chankey Pathak",
    "loginid": "chankeypathak",
    "password": "tutswiki"
}

config_object["SERVERCONFIG"] = {
    "host": "tutswiki.com",
    "port": "8080",
    "ipaddr": "8.8.8.8"
}

#Write the above sections to config.ini file
with open('config.ini', 'w') as conf:
    config_object.write(conf)

【问题讨论】:

  • 您的 IDE 是否配置为特定的古代 Python 版本? with 语句于 2006 年在 Python 2.5 中添加。仔细检查 IDE 中的预期版本。

标签: python with-statement


【解决方案1】:

谢谢你 Silvio,你把我推向了正确的方向。

我将 Python 3.9(64 位)添加到我的项目中。在安装 Visual Studio 时,我还选择了 Python3 的 64 位版本(不是可用的最新版本--> 3.7.8)。

但是Visual Studios的安装路径是:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe

由于“(x86)”,我在安装 Visual Studio 时尝试安装 32 位 Python3。

现在可以正常使用了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多