how to tell pylint to ignore certain imports?

Question:

I’m developing software for Windows with Python. I am developing on Linux, and I am using
Pylint to check my code.
I can’t get rid of the error:

F| Unable to import '_winreg'   

This is obvious – Python on Linux does not have this module.

So, what do I have to put in my .pylintrc to ignore this error?

Thanks in advance,
Oz

EDIT:

Documentation says:

:F0401: *Unable to import %r*
  Used when pylint has been unable to import a module.

Now I need to find how to use it …

Partial solution:

pylint --disable=F0401 <filename>

I am still looking for a way to do via .pylintrc.

Asked By: oz123

||

Answers:

[Edit: This is not the wanted solution since a change in the pylint check file is requested, but I leave it in case the code itself can be changed, which can not after a comment]:

Put a try/except block around the import statement.

Or even better. something like:

CONFIG = 'Unix'


if CONFIG == 'Unix':
    import  UnixLib
elif CONFIG == 'Win':
    import  WinLib
else:
   assert False
Answered By: Michel Keijzers

For those who really want to ignore modules, I am putting here my little patch for pylint:
In ‘/pylint/checkers/imports.py’

262     def get_imported_module(self, modnode, importnode, modname):
+263         import sys
+264         ignoreModules = ['_winreg', 'your', 'bogus','module','name']
265         try:        
+266             if sys.platform =='linux2' and modname not in ignoreModules:
267                 return importnode.do_import_module(modname)
268         except astng.InferenceError, ex:
269             if str(ex) != modname:
270                 args = '%r (%s)' % (modname, ex)

This little hack does the job better then just ignoring all warnings. Optimally, if I will have the time I will put a patch to do it via the .pylintrc file.

Answered By: oz123

A solution that I have seen employed at my workplace, where there is a special module which Pylint can’t possibly get at (Python is embedded and this special module is inside the main executable, while pylint is run in a regular Python installation) is to mock it by creating a .py file and putting it in the python path when running pylint (see PyLint "Unable to import" error – how to set PYTHONPATH?).

So, you might have a “pylint-fakes” directory containing an empty _winreg.py (or if you need to check imported names, not empty but with faked variables).

Answered By: Chris Morgan

Just run into this as well with the following code:

 8: if os.name == 'nt':
 9:    import msvcrt
10: else:
11:    import fcntl

pylint failed the build with this error:

E:  9, 4: Unable to import 'msvcrt' (import-error)

The solution is available since pylint 0.10:

 9:    import msvcrt  # pylint: disable=import-error
Answered By: anatoly techtonik

Question is quite old, but right now you can ignore modules with .pylintrc like:

ignored-modules=module1,module2,...

I’ve used it to suppress uninstallable modules check with third-party CI tools, and it works just fine.

Answered By: a5kin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.