16

I'm using pylint + pydev, with python 2.6. I have a module with just this line of code

from email import Message

Now when I try to run this module it runs fine. But pylint reports an error:

ID: E0611 No name 'Message' in module 'email'

Although it exists... Any idea why?

balpha
  • 50,022
  • 18
  • 110
  • 131
olamundo
  • 23,991
  • 34
  • 108
  • 149
  • 2
    Also occurs with `lxml.etree`. – pfctdayelise Mar 29 '11 at 23:34
  • One day my code worked. The next it didn't and was complaining about this. I deleted the `_pycache_` folder and re-ran the project. That solved it for me. Some sort of corruption I guess. – JGFMK May 15 '18 at 14:26
  • Have since read here http://docs.python-guide.org/en/latest/writing/gotchas/#bytecode-pyc-files-everywhere : export PYTHONDONTWRITEBYTECODE=1 (ie Environment Variable in Window speak) – JGFMK May 17 '18 at 21:57

3 Answers3

15

realise this is an old question, but the correct answer is that the older ways of invoking what you need, which use the "import hackery" that Richie describes, have long been deprecated (despite still appearing in many tutorials). If you use the new ways, you'll be writing better code and pylint won't complain.

e.g.

from email import Message
from email import Header
from email.MIMEText import MIMEText

should be

from email.message import Message
from email.header import Header
from email.mime.text import MIMEText

etc.

simon
  • 15,344
  • 5
  • 45
  • 67
  • Can you reference a PEP or some source where this was recommended? – Lukas Jul 12 '16 at 13:17
  • @Lukas -- it's not a PEP thing, it's an `email` package thing; this post is five years old, so I can't remember a specific discussion of old-style vs. new style, but you'll see the new style I recommend here used throughout the `email` package documentation (e.g. https://docs.python.org/2.7/library/email-examples.html). – simon Jul 13 '16 at 18:07
12

I like pylint, but I do find I have to use a lot of # pylint: disable-msg=E0611 and the like to make it shut up in cases that are perfectly correct but confuse it (for example, like in this case, due to email's playing with import tricks).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • I'd say here it is the fault of `email`, as it should not play with import hooks. – gb. Nov 16 '16 at 04:35
1

The email module uses some horrible import hackery, which has bitten me in the past. You can do this:

>>> from email import Message

but you can't do this:

>>> import email
>>> email.Message
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'Message'

I realise that's not very helpful for making pylint work, but it might help to explain the problem.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399