How to compile a python script/ program into an exe

Posted on January 2nd, 2011

In this little tutorial I will show you how to compile your wonderful Python program into a exe using a few steps only, I will also show you how to make a setup.exe that will allow others to install your program to their computers.

Step 1: Download and Install py2exe

  • You can download this from here
  • Step 2: Create a file in your python program workspace directory called setup.py

  • This directory depends on the IDE your using, if your using IDLE this is the directory your python program is located in. I use Aptana, so my directory was something like this: ‘C:\Users\Ronald\Documents\Aptana Studio 3 Workspace\PythonProgram
  • Step 3: Paste this code into your new setup.py file

  • Where you see ‘main.py’ change that to the exact location of your main program file
  • 1
    2
    3
    from distutils.core import setup
    import py2exe
    setup(console=['main.py'])

    Step 4: Create another file from Notepad called ‘compile.bat’, and save this to your program directory as well

  • Inside the compile.bat copy and paste this code below, and where the first line below says path, change that to the path of where you installed Python
  • 1
    2
    3
    4
    path c:\Python27\;%path%
    python print("Starting to compile...")
    python "setup.py" py2exe
    python print("Compile complete")

    Step 5: Run compile.bat

  • Just double click it, lol
  • That’s it! Now you have your python program as an executable! :)

    Now this next part is for those who want to create an installer for their new executable…

    Step 1: Download Inno Setup Compiler

  • You can get that here
  • Step 2: Run Inno Setup Compiler, go through the options and select your new exe, compile the inno script and save if you like

  • Now remember, if your python exe had a bunch of library dependencies and folders once it was created, don’t forget to include those as well in your installer script when your selecting your python exe. Your new exe needs those files and folders to run properly.
  • That’s it! Now you should be left with a setup.exe, which will install your python program.

    Screenshots coming soon… enjoy!


    Leave a Reply

      To syntax highlight code just use [cc lang="whatever language here"]your code here[/cc]