By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

python import problem (different versions of python)?

P: 2
Newbie here with a (hopefully not) dumb question....tried searching around on the 'Net without success, so here goes....

I have been playing around with python. I wrote a script that sends an smtp email. Here is the link that I found explaining how such a script could be written Sending an Email

As you see it imports a module as follows: "import smtplib".

I am using a linux machine (ubuntu ditro). It has both python2.5 and python2.4 installed. I also have wxpython installed.

Here is the script:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/env python
  2.  
  3. def Email_func (Mess):
  4.  
  5.     import smtplib
  6.  
  7.     HeaderFile = '/home/***.***'
  8.     Header = open(HeaderFile, 'r')
  9.     MyAddress = Header.readline()
  10.     OppAddress = Header.readline()
  11.     Subject = Header.readline()
  12.     EmailFull = ''.join([MyAddress, OppAddress, Subject, Mess])
  13.  
  14.     # smtp sever settings
  15.  
  16.     smtpserver = 'mydomain.com'
  17.     AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
  18.     smtpuser = 'myname@mydomain.com'  # for SMTP AUTH, set SMTP username here
  19.     smtppass = '****'  # for SMTP AUTH, set SMTP password here
  20.  
  21.     # creating email address settings
  22.  
  23.     RECIPIENTS = [OppAddress, MyAddress]
  24.     SENDER = MyAddress
  25.  
  26.     # sending email
  27.  
  28.     session = smtplib.SMTP(smtpserver)
  29.     if AUTHREQUIRED:
  30.         session.login(smtpuser, smtppass)
  31.         print "Email successfully sent!"
  32.     smtpresult = session.sendmail(SENDER, RECIPIENTS, EmailFull)
  33.  
  34.     if smtpresult:
  35.         errstr = ""
  36.         for recip in smtpresult.keys():
  37.             errstr = """Could not delivery mail to: %s
  38.  
  39.     Server said: %s
  40.     %s
  41.  
  42.     %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
  43.         raise smtplib.SMTPException, errstr
From the terminal, the script works fine. I then played around with tkinter, and I got the script to work from a GUI as well.

However, I then tried playing around with wxpython and when I tried to send the email thru the wxpython created GUI, I get the following error message:

Traceback (most recent call last):
File "/home/***/python_scripts/glade/email.py", line 40, in save
SendGame_func.EmailGame(self.text_ctrl_1.GetValue( ))
File "/home/****/python_scripts/glade/SendGame_func.py", line 5, in EmailGame
import smtplib
File "/usr/lib/python2.4/smtplib.py", line 49, in ?
from email.base64MIME import encode as encode_base64
ImportError: No module named base64MIME
I have looked around in my /usr/lib directory, and there is an smtplib in a python2.4 directory and one in the python2.5 directory.

I tried changing:

#! /usr/bin/env python
to

#! /usr/bin/python2.4
In both the above script and the GUI scripts. Result is the same, so I changed it to:

#! /usr/bin/python2.5
With the result that the wxpython script tells me:

Traceback (most recent call last):
File "/home/****/python_scripts/glade/email.py", line 5, in <module>
import wx
ImportError: No module named wx
Why am I getting this error in wxpython, but not tkinter and the terminal?
May 15 '07 #1
Share this Question
Share on Google+
3 Replies


bartonc
Expert 5K+
P: 6,596
Newbie here with a (hopefully not) dumb question....tried searching around on the 'Net without success, so here goes....

I have been playing around with python. I wrote a script that sends an smtp email. Here is the link that I found explaining how such a script could be written Sending an Email

As you see it imports a module as follows: "import smtplib".

I am using a linux machine (ubuntu ditro). It has both python2.5 and python2.4 installed. I also have wxpython installed.

Here is the script:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/env python
  2.  
  3. def Email_func (Mess):
  4.  
  5.     import smtplib
  6.  
  7.     HeaderFile = '/home/***.***'
  8.     Header = open(HeaderFile, 'r')
  9.     MyAddress = Header.readline()
  10.     OppAddress = Header.readline()
  11.     Subject = Header.readline()
  12.     EmailFull = ''.join([MyAddress, OppAddress, Subject, Mess])
  13.  
  14.     # smtp sever settings
  15.  
  16.     smtpserver = 'mydomain.com'
  17.     AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
  18.     smtpuser = 'myname@mydomain.com'  # for SMTP AUTH, set SMTP username here
  19.     smtppass = '****'  # for SMTP AUTH, set SMTP password here
  20.  
  21.     # creating email address settings
  22.  
  23.     RECIPIENTS = [OppAddress, MyAddress]
  24.     SENDER = MyAddress
  25.  
  26.     # sending email
  27.  
  28.     session = smtplib.SMTP(smtpserver)
  29.     if AUTHREQUIRED:
  30.         session.login(smtpuser, smtppass)
  31.         print "Email successfully sent!"
  32.     smtpresult = session.sendmail(SENDER, RECIPIENTS, EmailFull)
  33.  
  34.     if smtpresult:
  35.         errstr = ""
  36.         for recip in smtpresult.keys():
  37.             errstr = """Could not delivery mail to: %s
  38.  
  39.     Server said: %s
  40.     %s
  41.  
  42.     %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
  43.         raise smtplib.SMTPException, errstr
From the terminal, the script works fine. I then played around with tkinter, and I got the script to work from a GUI as well.

However, I then tried playing around with wxpython and when I tried to send the email thru the wxpython created GUI, I get the following error message:



I have looked around in my /usr/lib directory, and there is an smtplib in a python2.4 directory and one in the python2.5 directory.

I tried changing:



to



In both the above script and the GUI scripts. Result is the same, so I changed it to:



With the result that the wxpython script tells me:



Why am I getting this error in wxpython, but not tkinter and the terminal?
wx installs in the site-packages directory of the python version that is is compiled to run under (they're different). You'll need to have each wx site packagd installed in order to be able to switch versions of the interpreter.

Hope that helps,
Barton
May 15 '07 #2

P: 2
wx installs in the site-packages directory of the python version that is is compiled to run under (they're different). You'll need to have each wx site packagd installed in order to be able to switch versions of the interpreter.

Hope that helps,
Barton
Yes, that is helpful. But what file am I looking for in "site-packages?" In the python2.5 directory there is no file or subdirectory that seems to reference wx. In python2.4, there are several files that seem to be wx related, plus a wxglade directory

Interestingly, the wxpython GUI that I am having ptroblems with was created with wxglade. After I posted above post, I added a email choice to a menu to a GIU that I hand coded with wxpython (the GUI was based on one I created following along this tutorial. That script calls the same email function as above, and it worked fine?

Is this just a thing with wxglade? Not sure what is in the source that makes the wxglade created script behave differently? All "imports" etc seem to be the same as the other wxpython script?

Thanks for bearing with me :-)
May 16 '07 #3

bartonc
Expert 5K+
P: 6,596
Yes, that is helpful. But what file am I looking for in "site-packages?" In the python2.5 directory there is no file or subdirectory that seems to reference wx. In python2.4, there are several files that seem to be wx related, plus a wxglade directory

Interestingly, the wxpython GUI that I am having ptroblems with was created with wxglade. After I posted above post, I added a email choice to a menu to a GIU that I hand coded with wxpython (the GUI was based on one I created following along this tutorial. That script calls the same email function as above, and it worked fine?

Is this just a thing with wxglade? Not sure what is in the source that makes the wxglade created script behave differently? All "imports" etc seem to be the same as the other wxpython script?

Thanks for bearing with me :-)
The wx installation should be 1 directory (sorry, I'm not on my development machine at the moment) plus one .pth file that points to that directory and (I think) there's something like wxaddons directory (don't know what's in there).

Also, I'm on windows so the help that I can offer is limited in this regard.

I had problems with wxGlade on windows. I switched to Boa Constructor a long time ago. Boa is TRULY a miraculous piece of work!
May 17 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.