如何获得pylint识别MySQLdb成员? [英] How do I get pylint to recognize MySQLdb members?

查看:107
本文介绍了如何获得pylint识别MySQLdb成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import MySQLdb

try:
    dbcon = MySQLdb.connect(host=host_name, user=user_name,
                                     passwd=password, db=db_name)
except MySQLdb.Error:
    pass

获取此警告信息 模块"MySQLdb"没有错误"成员(无成员)

getting this pylint warning Module 'MySQLdb' has no 'Error' member (no-member)

推荐答案

最佳:

使用 extension-pkg-whitelist选项:

以逗号分隔的程序包或模块名称列表,可从中加载C扩展名.扩展程序正在加载到活动的Python解释器中,并且可以运行任意代码

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=_mysql

PyLint 解析(默认情况下)源文件,但是在Python中,模块的形状可以更改为运行时从源文件中定义的形状开始.此选项告诉 PyLint 实际导入指定的模块,并使用运行时定义.

PyLint parses (by default) the source files, but in Python the shape of a module can change at runtime from the shape defined in the source file. This option tells PyLint to actually import the specified module, and use the runtime definition.

请注意,由于 MySQLdb 包包装了C扩展名,因此您必须传递C扩展名(_mysql)而不是程序包名称(MySQLdb). ()

Note that since the MySQLdb package wraps a C extension, you have to pass the name of the C extension (_mysql) instead of the package name (MySQLdb). (source)

还不错:

使用 unsafe-load-any-extension选项

允许加载任意C扩展名.扩展被导入到活动的Python解释器中,并且可以运行任意代码.

Allow loading of arbitrary C extensions. Extensions are imported into the active Python interpreter and may run arbitrary code.

--unsafe-load-any-extension=yes

您可以使用unsafe-load-any-extension选项,但是它将加载每个可用的扩展名及其初始化代码(可能有危险). extension-pkg-whitelist更安全,因为它仅加载指定的模块.

You could use the unsafe-load-any-extension option, but that would load every available extension, with its' (potentially dangerous) initialization code. extension-pkg-whitelist is safer, because it only loads the specified modules.

最差:

使用 disable选项

# pylint: disable=no-member

它并不能真正解决问题,只能使 PyLint 保持沉默.

It doesn't really solve the issue, but only makes PyLint silent.

感谢PyLint的维护者 @PCManticore . 在这里是维护者的评论.

Thanks to @PCManticore, the maintainer of PyLint. Here's the comment of the maintainer.

感谢 @ZevSpitz ,这是最佳答案的贡献者,而本<强>不错答案.

Thanks to @ZevSpitz, the contributor of the best answer and this not bad answer.

这篇关于如何获得pylint识别MySQLdb成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆