我正在使用 astropy 的单位和 EartLocation 来设置我的天文台位置。暂时硬编码。
但是,当我在 VScode 中运行代码时,pylint 出现错误,而当我在 PyCharm 和 IDLE 中运行时,代码运行良好。
代码:
obsy_location = EarthLocation(lat=52.91044*units.deg, lon=5.242664*units.deg, height=0*units.m)
Pylint 在那一行代码中给我打了 3 个错误:E1101:Module 'astropy.units' has no 'deg' member; maybe 'dex'?
E1101:Module 'astropy.units' has no 'm' member; maybe 'g'?
E1101:Module 'astropy.units' has no 'hour' member
请注意,我是初学者,但由于 PyCharm 和 IDLE 似乎在运行这条线时都没有任何问题,我想知道 VScode 的 pylint 在这里做了什么。有人可以启发我吗?
我使用最新的 Anaconda 解释器运行 VScode。
最佳答案
pylint 或 PyCharm 等工具进行静态代码分析。对于大多数 Python 代码,它们通常做得很好,但在使用 Python 的动态特性的情况下它们会失败。
在这种情况下,astropy.units
在导入时执行 Python 代码以生成单元对象,如 deg
(代表单位“度”)或 m
(代表单位“米”)。从这个意义上说,您看到“astropy.units has no member deg”警告是正常的,因为静态分析工具不执行导入,因此这些东西不存在于它们的分析中。我看到来自 PyCharm 的相同警告(它有自己的静态分析,不像你的情况那样在后台执行 PyLint)。
现在总是可以配置静态分析工具来忽略某些类的警告。看着 https://stackoverflow.com/a/39500741/498873 ,我希望这个 VS Code 配置设置能够做到这一点(但没有尝试):
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=astropy.units"
]
关于visual-studio-code - VScode (?) 中astropy 的pylint 错误不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49846940/