Pylint: [no-meber] Module 'MySQLdb' has no 'ProgrammingError' member

Created on 19 Oct 2016  路  11Comments  路  Source: PyCQA/pylint

Steps to reproduce

imort MySQLdb as db
try:
   cur.execute(sql)
except db.ProgrammingError, err:
   logging.error(err)

Current behavior

[no-meber] Module 'MySQLdb' has no 'ProgrammingError' member

Expected behavior

No error occurs, MySQLdb has ProgrammingError attribute actually.

pylint --version output

pylint 1.5.6, 
astroid 1.4.8
Python 2.7.12 (default, Jul 27 2016, 16:11:41) 
[GCC 5.4.0]

Most helpful comment

@zspitz yes, the name of the C extension has to be passed to extension-pkg-whitelist whenever a python library wraps it. This is a caveat that I'll hope to address with #1962.

All 11 comments

This seems to be an extension package. Pylint cannot easily understand them, providing only two useful tricks you can use:

  • you can pass --extension-pkg-whitelist=MySQLDB to pylint when you execute it. This will force pylint to load your module and try to build a dynamic AST from the live objects. This might not work, depending how that module is structured or might not be desired at all, since it imports a C extension.

  • you can write an astroid brain tip (https://github.com/PyCQA/astroid/tree/master/astroid/brain) for MySQLdb, in which you can provide any object that pylint cannot recognize.

Hope this helps.

Unfortunately, --extension-pkg-whitelist=MySQLDB doesn't work for me. pylintrc file with extension-pkg-whitelist=MySQLDB doesn't work either. I've tried MySQLdb, too, but still I have the pylint error.

pylint 1.8.3,
astroid 1.6.2
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516]

Also, isn't this duplicated https://github.com/PyCQA/astroid/issues/56 ?
According to this, the issue should have been fixed already. But apparently, it's not.

According to https://github.com/PyCQA/astroid/issues/56, adding whitelist by writing an astroid brain tip seems discouraged. But is the policy changed?

I couldn't find the reason why extension-pkg-whitelist doesn't work for me, but I found another solution.
unsafe-load-any-extension=yes is the thing.

The thing is that unsafe-load-any-extension and extension-pkg-whitelist is basically the same feature, with the caveat that the former will exempt all C extensions, while the latter will exempt only those mentioned that list. It could be that with unsafe-load-any-extension we also load some module that we miss with extension-pkg-whitelist which could explain why one works for you but the other does not.

@PCManticore Thx. Apparently, the module name that's recognized by astroid is not MySQLdb, but _mysql. But it doesn't look right. Isn't it supposed to recognize MySQLdb, especially, the target python code imports MySQLdb rather than _mysql directly?

@soundlake That happens because MySQLDb has a C extension that it wraps (https://github.com/farcepest/MySQLdb1/blob/master/_mysql.c). extension-pkg-whitelist unfortunately needs the actual C modules to build the AST.

@PCManticore Thank you for the explanation. I'm not pond of the situation, but I get the point.

@PCManticore Does the same thing (the name of the C extension must be used with extension-pkg-whitelist instead of the Python package`) happen every time a package wraps a C extension? If not, under what circumstances does this happen?

@zspitz yes, the name of the C extension has to be passed to extension-pkg-whitelist whenever a python library wraps it. This is a caveat that I'll hope to address with #1962.

I had the same issue, but extension-pkg-whitelist=MySQLdb works fine for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thanatos picture thanatos  路  3Comments

PCManticore picture PCManticore  路  3Comments

pylint-bot picture pylint-bot  路  3Comments

sambarluc picture sambarluc  路  3Comments

TBoshoven picture TBoshoven  路  3Comments