PyLint 不识别 cv2 成员

PyLint not recognizing cv2 members

我在一个 opencv 项目中 运行 pylint,我在 VS 代码中收到许多关于成员不存在的 pylint 错误。

示例代码:

import cv2
cv2.imshow(....)

获得的错误:

但是,代码运行正确,没有任何错误。

版本:pylint 1.8.1,astroid 1.6.0

是的,因为没有安装扩展。 设置:extension-pkg-whitelist=cv2 就可以了。 但是它可能无法检测到 cv2

中实现的功能或模块

这是来自pylint。您可以使用以下命令在项目的根目录中生成一个 pylint 配置文件: (如果您在团队中工作或在同一存储库的不同计算机上工作,我发现这很有用)

pylint --generate-rcfile > ~/.pylintrc

在生成的 .pylintrc 文件的开头你会看到

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-whitelist=

添加 cv2,这样你就可以得到

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-whitelist=cv2

保存文件。 lint 错误应该会消失。

此处是 MS V 代码中 settings.json 文件的代码片段

"python.linting.pylintArgs":["--extension-pkg-whitelist=cv2"]

我在 vscode 的 settings.json 中使用了以下配置设置,它帮助我避免了 pylint 的不必要标志,并且还获得了 cv2 工作的智能感知, 它不起作用尝试从 C:\Anaconda3\envs\demo1\Lib\site-packages 文件夹中卸载和删除 cv2 包,然后重新安装 opencv-python package

{
"python.linting.pylintEnabled": true,
  "python.linting.enabled": true,
  "python.linting.pylintArgs": [
    "--extension-pkg-whitelist=cv2"
  ]
}
  1. 在 VScode 上:CTRL + Shift + P
  2. 选择"Preferences: Open Settings (JSON)"
  3. 将此行添加到 JSON 文件中: "python.linting.pylintArgs": ["--generate-members"]

完成,对我有用

注意:确保选择 "Preferences: Open Settings (JSON)",而不是 "Preferences: Open Default Settings (JSON)"

设置文件看起来像

{
"workbench.iconTheme": "vscode-icons",
"python.dataScience.sendSelectionToInteractiveWindow": true,
"kite.showWelcomeNotificationOnStartup": false,
"python.dataScience.askForKernelRestart": false,
"python.dataScience.jupyterServerURI": "local",
"python.pythonPath": "/usr/bin/python3",
"workbench.colorTheme": "Monokai",
"vsicons.dontShowNewVersionMessage": true,
"python.linting.pylintArgs": ["--generate-members"] }

尝试像这样导入 cv2:from cv2 import cv2.

我不需要像这里的大多数答案那样更改 pylint Jason 文件中的任何内容我的解决方案是将导入语句更改为以下形式

from cv2 import cv2

终于可以使用cv2成员了!