Thread: py2exe and

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Location
    Amsterdam, the Netherlands
    Posts
    12
    Rep Power
    0

    py2exe and external files


    I've only recently started working with Python, so since this is all fairly new to me forgive me for asking a beginner question.

    1. My module makes use of the modules pyTTS , which requires the Microsoft Speech API (SAPI), and pySonic , which requires a specific DLL file (fmod.dll) to be present in the system32 folder. I've installed these things and it works fine
    However:

    I use py2exe to create a standalone version of my program, using the following setup script:

    Code:
    from distutils.core import setup
    import py2exe
    setup(name='test', version='0.1.0', py_modules=['test'],windows=['test.py'])
    I then create the standalone version using

    Code:
    python setup.py py2exe
    This executes without errors, but when I want to open the created exe in the 'dist' folder and error is logged, which is either

    Traceback (most recent call last):
    File "test.py", line 19, in ?
    File "pyTTS\__init__.pyc", line 28, in Create
    ValueError: "SAPI" not supported

    or

    Traceback (most recent call last):
    File "test.py", line 11, in ?
    File "pySonic.pyx", line 975, in pySonic.FileSample.__new__
    pySonic.FMODError: File not found.


    depending on which occurs first. These errors normally occur when the abovementioned files (SAPI and fmod.dll) are not present, which means that whoever wants to use my standalone version must first install them. But the problem here is that they are already installed and yet I still get the errors. This doesn't happen when I run the original .py file using python, only when I run the exe created by py2exe.

    Of course this problem doesn't really have anything to do with pySonic or pyTTS, but is rather related to the fact that somehow py2exe doesn't include access to files outside the package directory. Does anybody know to fix this?, I have so little experience with Python that it's difficult to find out what I'm doing wrong. I mean the files are there so I don't understand why they would be accessible through my original script and not through the standalone version of it.

    Thanks!
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    157
    Hmm, did you try reinstalling SAPI?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Location
    Amsterdam, the Netherlands
    Posts
    12
    Rep Power
    0
    For the record, I solved the problem although it took me quite a few hours to figure it out.

    It turns out that the above mentioned pyTTS module imports the win32com.client module (part of the Python Win32 Extensions, which allow interaction with the windows environment and COM objects). To communicate with a specific Windows application the makepy.py module (also part of the win32 extensions) has to make a module that maps to the application's library, which in this case was 'Microsoft Speech Object Library'. This generated .py file is not automatically included by py2exe, so you have to tell it to do so by adding an extra option to the setup call in your setup file. This option needs some specific values for the application in question which can be retrieved by running makepy.py with the -i flag

    so in this case I had to run:

    Code:
    python makepy.py -i "Microsoft Speech Object Library"
    which yielded:

    Code:
    Microsoft Speech Object Library
     {C866CA3A-32F7-11D2-9602-00C04F8EE628}, lcid=0, major=5, minor=0
     >>> # Use these commands in Python code to auto generate .py support
     >>> from win32com.client import gencache
     >>> gencache.EnsureModule('{C866CA3A-32F7-11D2-9602-00C04F8EE628}', 0, 5, 0)
    I then used these values in my setup call:

    Code:
    setup(	name='test', 
    		version='0.1.0', 
    		py_modules=['test'],
    		windows=['test.py'],
    		options={"py2exe": {"typelibs": [('{C866CA3A-32F7-11D2-9602-00C04F8EE628}',0,5,0)]}}
    		)
    This makes py2exe include the application specific file 'C866CA3A-32F7-11D2-9602-00C04F8EE628x0x5x0.py'.

    With this file present, everything works great. So, if you ever have to make use of win32 stuff and it isn't working after you use py2exe, check whether your modules access application specific data which require makepy to be run first.

    It could save you quite some time!

    Comments on this post

    • sf2k agrees : great information! may come in handy
    • Grim Archon agrees : Very useful, good solid info.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0
    PennyWaffer -

    Thank you so much - I can't think how many hours you must have saved me!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    6
    Rep Power
    0
    Originally Posted by Pennywaffer
    For the record, I solved the problem although it took me quite a few hours to figure it out.

    It turns out that the above mentioned pyTTS module imports the win32com.client module (part of the Python Win32 Extensions, which allow interaction with the windows environment and COM objects). To communicate with a specific Windows application the makepy.py module (also part of the win32 extensions) has to make a module that maps to the application's library, which in this case was 'Microsoft Speech Object Library'. This generated .py file is not automatically included by py2exe, so you have to tell it to do so by adding an extra option to the setup call in your setup file. This option needs some specific values for the application in question which can be retrieved by running makepy.py with the -i flag

    so in this case I had to run:

    Code:
    python makepy.py -i "Microsoft Speech Object Library"
    which yielded:

    Code:
    Microsoft Speech Object Library
     {C866CA3A-32F7-11D2-9602-00C04F8EE628}, lcid=0, major=5, minor=0
     >>> # Use these commands in Python code to auto generate .py support
     >>> from win32com.client import gencache
     >>> gencache.EnsureModule('{C866CA3A-32F7-11D2-9602-00C04F8EE628}', 0, 5, 0)
    I then used these values in my setup call:

    Code:
    setup(	name='test', 
    		version='0.1.0', 
    		py_modules=['test'],
    		windows=['test.py'],
    		options={"py2exe": {"typelibs": [('{C866CA3A-32F7-11D2-9602-00C04F8EE628}',0,5,0)]}}
    		)
    This makes py2exe include the application specific file 'C866CA3A-32F7-11D2-9602-00C04F8EE628x0x5x0.py'.

    With this file present, everything works great. So, if you ever have to make use of win32 stuff and it isn't working after you use py2exe, check whether your modules access application specific data which require makepy to be run first.

    It could save you quite some time!
    Have you tried running the binary on a few other Windows PCs with "Microsoft Speech Object Library" installed (if that's not installed by default by Windows)?

    The compilation process looks to me like it references a COM GUID, which I believe may be unique on each PC? So the GUID for "Microsoft Speech Object Library" may be different for different PCs. If that was true, one would need to rebuild or make the COM GUID python binding on each PC to deploy for.

    http://en.wikipedia.org/wiki/Globally_unique_identifier

    Hopefully, I'm wrong.

IMN logo majestic logo threadwatch logo seochat tools logo