VSCode 解决 Pylint 报错 No name QWidget in module PyQt5.QtWidgets

问题

启用了 VSCode 的 Pylint 插件, 即便 Python 环境中安装了 PyQt5, 也无法正确解析 PyQt5 的导入

在这里插入图片描述

PyQt5 底层代码是用 C/C++ 写的, pylint 默认不会深入解析

pylint doesn’t load any C extensions by default, because those can run arbitrary code.

解决

  1. 修改 Settings

在 settings.json 中附加入以下内容

"pylint.args": [
    "--extension-pkg-whitelist=PyQt5",
]

或者在图形界面找到并修改 pylint 的配置

在这里插入图片描述

以上针对的是新版本的 VSCode(单独安装了 pylint extension); 对于老版本(pylint 的功能是内置于 Python 插件), 则是以下配置

"python.linting.pylintArgs": [
    "--extension-pkg-whitelist=PyQt5",
]
  • 配置项的命名以 python 开头, 表示是 Python 插件的配置
  1. 如果不想修改 settings.json, 也可以在 .pylintrc 中加入以下内容
extension-pkg-whitelist=PyQt5

并确保 Pylint 插件能识别到该 .pylintrc 文件(一般放在项目的根目录下)

"pylint.args": [
	"--rcfile=${workspaceFolder}/.pylintrc"
]
E:\python3.9.13\python.exe E:\pycharm\study\计算机视觉\test3.py Traceback (most recent call last): File "E:\python3.9.13\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 73, in <module> from tensorflow.python._pywrap_tensorflow_internal import * ImportError: DLL load failed while importing _pywrap_tensorflow_internal: 动态链接库(DLL)初始化例程失败。 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "E:\pycharm\study\计算机视觉\test3.py", line 20, in <module> from 计算机视觉.test2 import ObjectRecognitionDeployer File "E:\pycharm\study\计算机视觉\test2.py", line 1, in <module> import tensorflow as tf File "E:\python3.9.13\lib\site-packages\tensorflow\__init__.py", line 40, in <module> from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow # pylint: disable=unused-import File "E:\python3.9.13\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 88, in <module> raise ImportError( ImportError: Traceback (most recent call last): File "E:\python3.9.13\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 73, in <module> from tensorflow.python._pywrap_tensorflow_internal import * ImportError: DLL load failed while importing _pywrap_tensorflow_internal: 动态链接库(DLL)初始化例程失败。 Failed to load the native TensorFlow runtime. See https://www.tensorflow.org/install/errors for some common causes and solutions. If you need help, create an issue at https://github.com/tensorflow/tensorflow/issues and include the entire stack trace above this error message.
最新发布
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值