34

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.

oz123
  • 27,559
  • 27
  • 125
  • 187

4 Answers4

36

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
anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
23

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.

a5kin
  • 1,335
  • 16
  • 20
  • 4
    I found that this needed to be in the IMPORTS section. – GreenMatt Jul 10 '18 at 19:30
  • 3
    @greenmatt It's in [TYPECHECK] in my ``.pylintrc``, maybe different version. – a5kin Jul 13 '18 at 21:45
  • It looks like this was added in 1.2.1, but updated to apply to imports in 1.4.4 (which is when it moved to the [IMPORTS] section. – ojrac Aug 16 '19 at 14:11
  • 1
    You can generate an empty .pylintrc file by `pylint-3 --generate-rcfile > ~/.pylintrc` – Aaron Swan Sep 04 '20 at 21:29
  • This works for me for exact module names. The documentation states that "It supports qualified module names, as well as Unix pattern matching.", but when I provide a wildcard name it doesn't seem to work. Seems there is an open issue: https://github.com/PyCQA/pylint/issues/8232 – jgoeders Feb 08 '23 at 20:11
10

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

Community
  • 1
  • 1
Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • Chris, thanks for you suggestion. I actually did the same for a module which is embedded in our work place too. My dummy module contains all the members of the real module. This is also a valid solutions. You get an up vote for that of course :-) – oz123 Mar 08 '12 at 12:13
8

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.

oz123
  • 27,559
  • 27
  • 125
  • 187
  • 1
    Have you ever found the time to submit that patch? – mic_e Jun 08 '15 at 15:46
  • 1
    It looks like the upcoming release of pylint 1.5.0 will contain a patch like this. – mic_e Jun 09 '15 at 20:00
  • Which cmd line option would that be, the [`ignored-modules`](https://pylint.pycqa.org/en/latest/user_guide/configuration/all-options.html#ignored-modules)? – Jens Jun 24 '22 at 11:43