Python Forum

Full Version: Building an exe with Py2exe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
There are numerous programs to aid in building an exe for Windows for your program... py2exePyInstaller, and cx_freeze are the top known libraries. py2exe is solely for Windows, while cx_freeze and PyInstaller can make a standalone for linux, mac, and windows. py2app is for making Mac OS X applications.

Quote:UPDATE: In recent years, Pyintaller has become the lead library (in my opinion) to build your exe's. Its practically so simple that there is not even a point to making a tutorial on here as it would be too short. Its currently updated, and works across all the major operating systems.

source for py2exe not working after python3.4?
(Feb-28-2019, 01:36 PM)snippsat Wrote: [ -> ]Not a source,but i know why. What’s New for Python 3.5
Quote:Windows improvements:
  • A new installer for Windows has replaced the old MSI. See Using Python on Windows for more information.
  • Windows builds now use Microsoft Visual C++ 14.0, and extension modules should use the same.
This mean that all libraries that build Freezing tools,has to make changes to adapt to this change. My guess is that that py2exe has lost there main developer/developers, then no one looking into at this for make changes that is required for it to work for Python 3.5-->.

In this example we will start off with py2exe. It can be downloaded from pypi. It is installed onto python as any other 3rd party library. You can use the msi installer, the wheel install, pip to install, or install via module source distribution. The latter one, you can download py2exe source (the zip file from Download link). Extract the zip file. Execute the followiing command in the windows shell (command prompt) to install the 3rd party library.

import os
import sys
import py2exe
from distutils.core import setup
 
origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
    dlls = ("libfreetype-6.dll", "libogg-0.dll", "sdl_ttf.dll")
    if os.path.basename(pathname).lower() in dlls:
        return 0
    return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL
 
sys.argv.append('py2exe')
setup(options={'py2exe': {'bundle_files': 1, 'compressed': True}},
      windows=[{'script': "game.py"}],
      zipfile=None)
This script can be modified to do different things. This is your "settings" to how you want the exe built. In this example everything is bundled into the exe. Everything except any resources that are required by your programs such as images, fonts, sounds, music, etc. The Python standard library is embedded into the exe, as well as the interpreter. This is indicated by zipfile being set to None, and the options dict of bundle_files set to 1, and compressed set to True. If you didnt do this you would have in addition to your exe a zip file with all the standard library, a python dll for your interpreter, that would have to be with your exe. "windows" is the arg for GUI programs. "game.py" is the starter script for your game (whatever script you execute to start your game). Save this script as "setup.py".

Now we are going to back to the command prompt and start the build. All you are going to do is execute your setup script with py2exe argument.

python setup.py py2exe


This should output some things on your command prompt and leave you with a "dist" and "build" directory created in the same directory of your setup.py script you just executed. Within the dist directory is your exe. Now move this exe to your starter script directory. This is because your program is looking for resources from where your starter script is located. Thus your exe will also be looking for the same paths to your resources. After this point you can delete the dist and build directories and test your exe.

So if your program structure is like

MyProgram/
    main.py
    ...
    resources/
        images/
           ...
        fonts/
           ...
If your starter script is main.py, then your exe needs to be where main.py is to look for the resources directory.

Troubleshooting problems

There are a lot of times you have errors or your exe just doesnt work. Normally this is only done once. Once you find a series of python, pygame, py2exe, DLLS, setup script, etc. that works you leave it be and keep using it. Every time you modify the setup.py file, you must save it and rerun python setup.py py2exe to rebuild the exe. And you should delete the dist and build directories of the previous build as to not confuse you.



MSVCR90.dll missing

This means your missing the Microsoft Visual C++ 2008 Package asssociated for your python version. You can remedy this for yourself by putting this dll in your python DLL directory. An example would be C:\Python2.7\DLLs. Or add this dll with your exe script and it will load from there. MSVCR90.dll andMSVCR90.dll SP1. If both fail then google other versions of MSVCR90.dll and retry. That is not unheard of.

ImportError: No module named _view
This problem is easily fixed by adding import pygame._view into the top of your starter script for the actual building of the exe. You can remove it afterwords.