Saturday 2 November 2013

How to create exe file by py2exe



We can create exe file of any python script by number of ways. But I will prefer only py2exe for it.
Here are some common ones:
py2exe is better for make windows exe . so first download py2exe from below link

DOWNLOAD  py2exe -----Python2.7 for window 32 bit

DOWNLOAD py2exe------Python 2.7 for window 64 bit
Now  install py2exe in your system and create setup.py for creating exe file.

Sample.py
#!/usr/bin/env python

# base.py
import pygtk
pygtk.require('2.0')
import gtk

class Base:
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_icon_from_file('images/logo.png')
        self.window.set_title("Sample window")
        self.window.set_position(gtk.WIN_POS_CENTER)
        self.window.set_size_request(500, 275)
        self.window.set_resizable(False)
        frame=gtk.Frame("Hi guys how are you?")
        self.window.add(frame)
        frame.set_uposition(2,40)
        frame.show()
        self.window.show()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    base = Base()
    base.main()
Setup.py
from distutils.core import setup
import py2exe

dfiles = [('images', ["images/cream_dust.png "])]

excludes = ["pywin", "pywin.debugger"] # there will be more in real life...
option = dict(optimize=2,
           dist_dir="Demo Project",
           excludes=excludes,
           packages=["win32api"])

setup(
    windows=[{'script':'sample.py' }],
    console=[{'script':'sample.py' }],
    data_files=dfiles,
    zipfile=None,
    options={'py2exe':{'bundle_files':1,'compressed':1,"dll_excludes":[ "mswsock.dll", "powrprof.dll" ]}, 'py2exe':option},
)
      

After creating sample.py (for this file we will create exe) and setup.py (this is for exe file creation with distutils library).


dfiles—if any file you want to use in project like as image then u have to mention in dfiles.

Now open command prompt and go through your directory where you have kept files (sample.py , setup.py).


D:\project> Python setup.py install

D:\project> Python setup.py py2exe


It will create one folder name Demo project, this is exe folder where all dependency library files stored. Click on sample.exe then you will see window which you have made in sample.py

Thanks Guys

2 comments:

  1. Using PyQt4 / Python 3.4, and wanting a single executable (not a folder of files), I think that limits me to using py2exe instead of some of the other newbie-friendly gui-based tools (cx_freeze , pyInstaller, etc).

    Unfortunately, while my .py file runs fine when I execute it in tge IDLE GUI, it doesn't run from the console due to 'no module named xxxx' errors.

    I assume I have to get that to work first before running py2exe... but what makes it work from the IDLE gui and not via a command prompt?

    ReplyDelete
    Replies
    1. Actually I have done it in python 2.7 without any problem for making exe file via py2exe.Can you tell me exact what error you are getting so i can figure out easily.

      Delete