2
import MySQLdb

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

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

Prashant
  • 21
  • 5

2 Answers2

1

The Best:

Using extension-pkg-whitelist option:

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 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.

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)


Not Bad:

Using unsafe-load-any-extension option

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

--unsafe-load-any-extension=yes

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.


The Worst:

Using disable option

# pylint: disable=no-member

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


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

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

soundlake
  • 321
  • 4
  • 10
  • I suggest a minor clarification: _Because it requires the actual C extension, but the Python wrapper._ -- which does it require, the C extension or the Python wrapper? – Zev Spitz Mar 27 '18 at 21:22
0

It may help to use the --extension-pkg-whitelist option:

--extension-pkg-whitelist=_mysql

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.

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)

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.

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • As I mentioned on my answer, it should be `--extension-pkg-whitelist=_mysql`, instead of `MySQLdb` – soundlake Mar 27 '18 at 17:44
  • @soundlake Currently my answer doesn't add any information to your answer; but I feel it is better presented, and without the explicit edit history. If this answer is acceptable to you, I suggest you copy and paste it into your answer, and I will delete mine. – Zev Spitz Mar 27 '18 at 23:01
  • Thank you for your kind suggestion. For the sake of archiving, I've copied your answer into mine, and added a bit of decoration. – soundlake Mar 28 '18 at 08:44
  • 1
    @soundlake You don't need to worry about archiving; the entire history of questions and answers [is stored in SO](https://stackoverflow.com/posts/49432948/revisions). Also, while the thanks and credit are much appreciated, they are considered out of place on SO -- worthy questions and answers should be upvoted. Perhaps in the comments, or at most a note in ` tags. – Zev Spitz Mar 28 '18 at 08:50