Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pylint doesn't like pkg_resources.resource_filename

In my script I use:

python from pkg_resources import resource_filename

Both PyDev and pylint 0.23 complain about unresolved import here.

E0611 No name 'resource_filename' in module 'pkg_resources'

As I understand, this happens due to the fact that both PyDev and pylint perform only source code analysis, without actually trying to parse/compile it. And apparently pkg_resources does something special to define pkg_resources. The package and symbol are of course there, and whole thing works just fine. Two questions:

  • How can I convince both Eclipse and PyDev that they're mistaken?
  • What causes the problem in the first place? I haven't found other people having this kind of problem, and I doubt my situation is somewhat unique... :)

Python 2.7.1 (under OSX), distribute 0.6.19.

like image 447
yacoob Avatar asked Jun 13 '11 21:06

yacoob


2 Answers

You can load troublesome dynamic modules by modifying your project's pydev python interpreter definition. Configure the default list of 'forced built-ins' to include dynamically generated definitions you use. Forced built-ins are generated by shelling out and loading/inspecting dynamically generated classes.

In the python interpreter definition (preferences => pydev => interpreters => python interpreter), select your currently used interpreter for your pydev project. Select the Forced Builtins tab. Press the New... button, and add pkg_resources to the list.

I've done this, and now my errors are gone, and auto-complete of methods works ok for my project. I'm using python 2.7.9, pydev 3.3.3, and eclipse kepler r2.

For pylint, you can disable the warning globally in your project's pylint config, for the entire file, or for one instance of its use. You can add a # pylint: disable=E1101 comment at start of your file to disable it for that module, or on on the line above where you use it to just disable it for that instance. There's also a possibly time-consuming way to manually hint to pylint that your class has specific methods.

like image 190
Epu Avatar answered Nov 15 '22 07:11

Epu


I had the same problem. For Pydev I found the answer on pydev.org: Go to the error line, hit ctrl-1, and select 'undefined variable'. It will then append a #@UndefinedVariable comment, and the error goes away.

For pylint, disabling E1101 does the trick, pragma # pylint: disable=E1101. Pylint pragmas just need to go at the same indent level, but the pydev comment had to be on the same line. My somewhat comment-cluttered function became:

def get_test_datafile(file_):
    # pylint: disable=E1101
    return pkg_resources.resource_string(__name__, #@UndefinedVariable
                                         'testdata/'+file_)
like image 25
André Lucas Avatar answered Nov 15 '22 05:11

André Lucas