954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

script wont run in IDLE, but individual lines run fine?

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_

rzufelt
Newbie Poster
3 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

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_

rzufelt
Newbie Poster
3 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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 packagecreated by you on the python path before you import smtplib.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

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_

rzufelt
Newbie Poster
3 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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

AcmeUK
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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


thanks :)

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You