问题描述
我将 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)
解决方法
谢谢 Silvio,你把我推向了正确的方向。
我在我的项目中添加了 Python 3.9(64 位)。在安装 Visual Studio 时,我还选择了 64 位版本的 Python3(不是可用的最新版本 --> 3.7.8)。
但是Visual Studios的安装路径是:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe
由于“(x86)”,我在安装 Visual Studio 时尝试安装 32 位 Python3。
现在一切正常!