Hi All,

If i copy/paste line for line the below code into IDLE (v2.5 or v2.6) it works perfectly and sends me the email message.

import smtplib

fromadd = "arcazy@foo.com"
toadd  = "meufelt@wfoo.com"
msg = "Insert message body here"
server = smtplib.SMTP('exchange01.foo.com')
server.sendmail(fromadd, toadd, msg)
server.quit()

However, If I try to open/run the .py script in IDLE (v2.5 or v2.6)(or with os.system(), I get the following error message:

Traceback (most recent call last):
  File "\\Wc98466\d\baks\dont_use\devel\email.py", line 1, in <module>
    import smtplib
  File "C:\Python25\lib\smtplib.py", line 46, in <module>
    import email.Utils
  File "\\Wc98466\d\baks\dont_use\devel\email.py", line 12, in <module>
    server = smtplib.SMTP('exchange01.foo.com')
AttributeError: 'module' object has no attribute 'SMTP'

Any ideas as to why these are running differently if I run from a file vs copy/pasting the code directly into IDLE as I am totally at a loss?

Thanks,

R_

As you can see, smtplib uses another module called 'email' which is a package in the standard library. I think you should rename your program to something else than 'email.py' (also why is your script in a folder called 'dont_use' ? Perhaps you should not use it :).

Edited 5 Years Ago by Gribouillis: n/a

Thanks,

I didn't even think about that, even though I've had issues with filenames before.
In this case, however, it doesn't seem to make a difference what I name the file.

It works just fine if I copy/paste into IDLE or cmd, but will not run no matter the filename.

Also, I am the one that named the folder dont_use to keep "others" out of there. It is specifically for my development so I will allow myself in this case :o)

Thanks again,

R_

The error message clearly shows that when smtplib runs the code import email.Utils , it imports your own "\\Wc98466\d\baks\dont_use\devel\email.py" instead of the package email of the standard library, which it must do. So the solution is to make sure that there is no file email.py or email.pyc or email package created by you on the python path before you import smtplib.

Edited 5 Years Ago by Gribouillis: n/a

Thought I had checked that and removed all "my" copies of email.py, but I noticed that a email.pyc showed up in there also. Was not looking for *.pyc files as not sure where it came from. In any case, I removed it and all is well.

Thanks again for the help,

R_

Hi Gribouillis

Many thanks for posting the above explanation.

I had the same problem, and renaming my Python script to myemail.py (and deleting email.py) solved the problem.

AcmeUK

This article has been dead for over six months. Start a new discussion instead.