Building executable - problem with MIME protocol

This is the place for queries that don't fit in any of the other categories.

Building executable - problem with MIME protocol

Postby escobar » Wed Feb 13, 2013 10:08 am

OK, so I'm a newbie. Please have understanding for my possible stupid questions.

I wrote a script which is retrieving some input from user, storing it into a file and than e-mails that file to me. All of that is working fine when I run *.py file. Data is stored, mail is sent. Everything looks perfect.
But, when trying to build executable I receive error message - "Import Error: No module named multipart". I tried removing the mail part from the script and than it successfully builds and program is working, as far as is retrieving data and storing to a file concerned.

Here is first part, which if excluded, allows successful build:

Code: Select all
import sys, gtk, pygtk
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
from smtplib import SMTP
import os


And here is mail definition:

Code: Select all
def mail(to, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = "Name Surname"
   msg['To'] = "recepient"
   msg['Subject'] = "SWLS"

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
         'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   mailServer.close()


One more thing to add, I managed to build executable under Linux, but since most of my possible users would use Windows I need to make *.exe file.
To summarise: *.py file is working both on Windows and Linux, while I only managed to build executable on Linux. I used cx_freeze for building.

Ask something if I missed to mention. Thank you for your help! :D
escobar
 
Posts: 7
Joined: Wed Feb 13, 2013 9:36 am

Re: Building executable - problem with MIME protocol

Postby stranac » Wed Feb 13, 2013 11:55 am

How are you running cx_freeze?
You might need to write a setup script.

Also, it's usually a good idea to post the entire error traceback, as it contains more information than just "Import Error: No module named multipart".
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1154
Joined: Thu Feb 07, 2013 3:42 pm

Re: Building executable - problem with MIME protocol

Postby escobar » Fri Feb 15, 2013 4:37 am

Ok. I used slightly different setup script, but now I've tried using that one from your link to cx_freeze's site with no additional options included. Now building is successful, but still getting the same error code when I start the built application. Here's the error message as attachment. Btw, main.py is my application script and line 6 says "from email.MIMEMultipart import MIMEMultipart". Thx!
Attachments
error message.jpg
error message.jpg (37.58 KiB) Viewed 1429 times
escobar
 
Posts: 7
Joined: Wed Feb 13, 2013 9:36 am

Re: Building executable - problem with MIME protocol

Postby micseydel » Fri Feb 15, 2013 4:48 am

It looks like you aren't providing your full code, and that could be a problem. Also, are you able to "import email" in Python 2.7 on your machine without problems?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Building executable - problem with MIME protocol

Postby escobar » Fri Feb 15, 2013 5:03 am

Actually, I wrote the script on Linux in Python 3.3, but since there's no PyGtk for Windows which is compatible with Python 3.3, I had to install py2.7 with pygtk2.7 and cx_freeze 2.7 to be able to run it on win. Maybe that could be the problem, but like I said in my first post - my script is doing fine when I run it from python shell - data stored, mail is sent. The problem arises when I try to build it. P.S. I could provide my full code but I just wanted to keep things simple as possible, since there's a only a lot of PyGtk stuff (labels, entries, buttons..). Salud!
escobar
 
Posts: 7
Joined: Wed Feb 13, 2013 9:36 am

Re: Building executable - problem with MIME protocol

Postby micseydel » Fri Feb 15, 2013 6:34 am

Rather than sending us the code you have now, you could take your local copy and reduce it and keep trying to build it until you've got the very least amount possible to reproduce your problem. (Sometimes doing this helps you find out exactly what the problem is and you don't need us; posting the solution would be appreciated!) You should probably start with the GUI (GTK) code (well-written code typically separates the logic from the interface, so if it's not easy, then you'll learn about what you would want in the future). Once you've got the minimalist problem, please post the exact code of your program and the build script as well as a description of the exact configuration that you have that is the problem - Python version, cxfreeze version, OS, whatever else might be useful.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Building executable - problem with MIME protocol

Postby escobar » Fri Feb 15, 2013 8:23 am

Well, I'll keep trying to solve the problem on my own, but if someone has enough time to take a look - here's my whole work.

1) My script (simplified). As a script file when ran from python shell, fully working both on Linux Mint Nadia and Windows 7 64bit. When built, only working on Linux - Windows displays MIME error which I included in previous posts.
Note that I tried changing python path (first line in code) to C:\Python27 when building in Windows. Doesn't work either.

Code: Select all
#!/usr/bin/python

import sys, gtk, pygtk
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
from smtplib import SMTP
import os

class base:
   
   def destroy (self,widget,data = None):
      gtk.main_quit()
      
   def enter_data(self,widget):
      age = self.age_spin.get_value()

      f = open("base.txt", "w")
      f.write("%s" % (self.name_entry.get_text() + "\t"))
      f.write("%s" % (self.surname_entry.get_text() + "\t"))
      f.write("%s" % (self.gender_combo.get_active_text() + "\t"))
                f.write("%s" % (age) + "\t")
      f.close()
   
   def __init__(self):
      self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
      self.window.set_size_request(600,250)
      self.window.set_title("DATA ENTRY")
      
      self.name = gtk.Label("Enter your name: ")
      self.name.set_alignment(10, 0.5)
      self.name_entry = gtk.Entry()
      
      self.surname = gtk.Label("Enter your surname: ")
      self.surname.set_alignment(10, 0.5)
      self.surname_entry = gtk.Entry()
      
      self.gender = gtk.Label("Choose gender: ")
      self.gender.set_alignment(10, 0.5)
      self.gender_combo = gtk.combo_box_new_text()
      self.gender_combo.append_text("MALE")
      self.gender_combo.append_text("FEMALE")
      
      self.age = gtk.Label("How old are you? ")
      self.age.set_alignment(10, 0.5)
      ad1 = gtk.Adjustment(0, 14, 100, 1, 0, 0)
      self.age_spin = gtk.SpinButton(adjustment=ad1, climb_rate=1, digits=0)
      
      self.enter = gtk.Button("ENTER")
      self.enter.connect("clicked", self.enter_data)
      
      self.box2 = gtk.HBox(homogeneous=True, spacing=0)
      self.box2.pack_start(self.name)
      self.box2.pack_start(self.name_entry)
      
      self.box3 = gtk.HBox(homogeneous=True, spacing=0)
      self.box3.pack_start(self.surname)
      self.box3.pack_start(self.surname_entry)

      self.box4 = gtk.HBox(homogeneous=True, spacing=0)
      self.box4.pack_start(self.gender)
      self.box4.pack_start(self.gender_combo)

      self.box5 = gtk.HBox(homogeneous=True, spacing=0)
      self.box5.pack_start(self.age)
      self.box5.pack_start(self.age_spin)

      self.box1 = gtk.VBox(homogeneous=True, spacing=0)
      self.box1.pack_start(self.box2)
      self.box1.pack_start(self.box3)
      self.box1.pack_start(self.box4)
      self.box1.pack_start(self.box5)
      self.box1.pack_start(self.enter)
      
      self.window.add(self.box1)
      self.window.show_all()
      self.window.connect("destroy", self.destroy)
      
   def main(self):
      gtk.main()

gmail_user = "username@gmail.com"
gmail_pwd = "password"

def mail(to, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = "Name Surname"
   msg['To'] = "somebody"
   msg['Subject'] = "subject"

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
         'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   mailServer.close()

base = base()
gtk.main()
mail("recepient@live.com",
   "Subject",
   "Here goes the message",
   "base.txt")


2) And here is my setup script. I'm using Python 2.7.3, PyGTK 2.24.1 for Python 2.7 and cx_Freeze 4.3.1 for Python 2.7.

Code: Select all
import sys
from cx_Freeze import setup, Executable

build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "swls",
        version = "0.1",
        description = "my stupid application",
        options = {"build-exe": build_exe_options},
        executables = [Executable("main.py", base=base)])


Now you can try the script for yourself if you enter some correct mail addresses..:D Try building and let me know if you had any success... Cheers! :mrgreen:
escobar
 
Posts: 7
Joined: Wed Feb 13, 2013 9:36 am

Re: Building executable - problem with MIME protocol

Postby stranac » Fri Feb 15, 2013 12:53 pm

The setup script in the link I gave you is just an example.
You need to modify it for your needs.
The link explains all the options and stuff.

Try using:
Code: Select all
build_exe_options = {"packages": ["email"]}
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1154
Joined: Thu Feb 07, 2013 3:42 pm

Re: Building executable - problem with MIME protocol

Postby escobar » Sat Feb 16, 2013 8:27 am

Ok, I tried adding that line, but it isn't helping. It says it is not a recognized module, skips it and keeps building the same way as before. I realize there are some options that I can configure in build script, but my program is very simple and I don't need anything special - just a simple .exe file. The error looks to me like it's pointing to some problem with Python27 libraries. But again, that's not explaining why am I able to run the script properly from Python shell.
I would appreciate if someone who's using Python 2.7 could try building my script on his system. Maybe you'll see something I don't see.. Thank you!
escobar
 
Posts: 7
Joined: Wed Feb 13, 2013 9:36 am

Re: Building executable - problem with MIME protocol

Postby stranac » Sat Feb 16, 2013 11:56 am

I tested, and this setup script works just fine for me, on Windows XP and Python 2.7:
Code: Select all
import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["email"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(name="cxtest",
      version="0.1",
      description="cx_Freeze email test",
      options={"build_exe": build_exe_options},
      executables=[Executable("cxtest.py", base=base)])
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1154
Joined: Thu Feb 07, 2013 3:42 pm

Re: Building executable - problem with MIME protocol

Postby escobar » Sun Feb 17, 2013 12:34 pm

Alright! The script didn't work for me again, but it pushed me forward to uninstall python and all of it's components, clear system registry and install everything all over again. And it worked then! Thank you for all of your support, finally did it!
Now I have one more thing that bothers me. Is it possible to somehow build a single executable that would work without all the dll's and pyd's and stuff? Thank you and please, forgive my ignorance. :)
escobar
 
Posts: 7
Joined: Wed Feb 13, 2013 9:36 am

Re: Building executable - problem with MIME protocol

Postby stranac » Sun Feb 17, 2013 6:01 pm

escobar wrote:Now I have one more thing that bothers me. Is it possible to somehow build a single executable that would work without all the dll's and pyd's and stuff?

No, not with cx_Freeze.
There are a few alternatives that can do this, such as py2exe, PyInstaller, bbfreeze...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1154
Joined: Thu Feb 07, 2013 3:42 pm

Re: Building executable - problem with MIME protocol

Postby escobar » Mon Feb 18, 2013 7:20 am

Thank you stranac, man! Bio si od pomoci..:)
escobar
 
Posts: 7
Joined: Wed Feb 13, 2013 9:36 am


Return to General Coding Help

Who is online

Users browsing this forum: nuclearpengu1n and 3 guests