python import problem (different versions of python)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gatoruss
    New Member
    • May 2007
    • 2

    #1

    python import problem (different versions of python)?

    Newbie here with a (hopefully not) dumb question....tri ed 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:

    Code:
    #! /usr/bin/env python
    
    def Email_func (Mess):
    
    	import smtplib
    
    	HeaderFile = '/home/***.***'
    	Header = open(HeaderFile, 'r')
    	MyAddress = Header.readline()
    	OppAddress = Header.readline()
    	Subject = Header.readline()
    	EmailFull = ''.join([MyAddress, OppAddress, Subject, Mess])
    
    	# smtp sever settings
    
    	smtpserver = 'mydomain.com'
    	AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
    	smtpuser = 'myname@mydomain.com'  # for SMTP AUTH, set SMTP username here
    	smtppass = '****'  # for SMTP AUTH, set SMTP password here
    
    	# creating email address settings
    
    	RECIPIENTS = [OppAddress, MyAddress]
    	SENDER = MyAddress
    	
    	# sending email
    
    	session = smtplib.SMTP(smtpserver)
    	if AUTHREQUIRED:
    	    session.login(smtpuser, smtppass)
    	    print "Email successfully sent!"
    	smtpresult = session.sendmail(SENDER, RECIPIENTS, EmailFull)
    
    	if smtpresult:
    	    errstr = ""
    	    for recip in smtpresult.keys():
    	        errstr = """Could not delivery mail to: %s
    
    	Server said: %s
    	%s
    
    	%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
    	    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.E mailGame(self.t ext_ctrl_1.GetV alue())
    File "/home/****/python_scripts/glade/SendGame_func.p y", line 5, in EmailGame
    import smtplib
    File "/usr/lib/python2.4/smtplib.py", line 49, in ?
    from email.base64MIM E 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?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by gatoruss
    Newbie here with a (hopefully not) dumb question....tri ed 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:

    Code:
    #! /usr/bin/env python
    
    def Email_func (Mess):
    
    	import smtplib
    
    	HeaderFile = '/home/***.***'
    	Header = open(HeaderFile, 'r')
    	MyAddress = Header.readline()
    	OppAddress = Header.readline()
    	Subject = Header.readline()
    	EmailFull = ''.join([MyAddress, OppAddress, Subject, Mess])
    
    	# smtp sever settings
    
    	smtpserver = 'mydomain.com'
    	AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
    	smtpuser = 'myname@mydomain.com'  # for SMTP AUTH, set SMTP username here
    	smtppass = '****'  # for SMTP AUTH, set SMTP password here
    
    	# creating email address settings
    
    	RECIPIENTS = [OppAddress, MyAddress]
    	SENDER = MyAddress
    	
    	# sending email
    
    	session = smtplib.SMTP(smtpserver)
    	if AUTHREQUIRED:
    	    session.login(smtpuser, smtppass)
    	    print "Email successfully sent!"
    	smtpresult = session.sendmail(SENDER, RECIPIENTS, EmailFull)
    
    	if smtpresult:
    	    errstr = ""
    	    for recip in smtpresult.keys():
    	        errstr = """Could not delivery mail to: %s
    
    	Server said: %s
    	%s
    
    	%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
    	    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

    Comment

    • gatoruss
      New Member
      • May 2007
      • 2

      #3
      Originally posted by bartonc
      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 :-)

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by gatoruss
        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!

        Comment

        Working...