# How can I use py2exe with the OSGeo4W Python installation?

I have a default Python installation in c:\Python27. I have also installed Python through the OSGeo4W installer, which has created a "sandboxed" Python installation with all the geospatial bindings I need. Now, I need to package a script using py2exe, but I can't figure out how to install or configure py2exe to work with the OSGeo4W installation. I have configured OSGeo4W to install external packages with pip, but py2exe doesn't seem to install with pip. If I try to use the downloaded installer, it only finds the system install in the registry. Any ideas how to get it working?

-

I've had better luck with pyinstaller than py2exe. It pretty much works out of the box for me with o4w python (you may need to install the optional python-win32 package). Follow the install instructions, and run the Build step. I didn't need to get into manually altering the spec file or even using options (other than --onefile for end user simplicity). Just make sure you run from inside the o4w shell so pyinstaller finds the right python environment. Here is my pyinstaller build batch file. There is only one line that does the real work:

python pyinstaller.py --onefile --out=%path\to\distrib%\myexe-%rev% %path\to\my\file.py%


Not to say py2exe can't work, it's just been so long now I no longer remember where I ran into difficulties with it or what the possible solution routes were.

-
Thanks, matt, this is a good alternative option and I'll give it a try. –  rudivonstaden Oct 2 '12 at 16:51
I ran into a snag with this approach: pyinstaller needs pywin32 to work on Python 2.6+. Fortunately it can be installed using the o4w setup program. –  rudivonstaden Oct 2 '12 at 18:49
I added note re: python-win32 requirement, thanks rudi –  matt wilkie Oct 2 '12 at 21:41
As a followup, I managed to create the exe without any problems using this method. When it came to running the executable, I kept getting ImportError: No module named _gdal. I thought it was a problem with the pyinstaller hooks, but it seems it's actually an environment issue in OSGeo4W, since I could get it to work fine from a regular cmd prompt (ie not o4w shell) as long as the GDAL_DATA environment variable is set. I wrote more on this at StackOverflow. –  rudivonstaden Oct 3 '12 at 8:15

one option would be to add the osgeo python to your windows registry: http://effbot.org/zone/python-register.htm

this site also contains information on how to remove it from the registry if you so desire.

I am pretty sure this will help you as long as you have admin rights to the registry.

-
Thanks, that would work, except for the following: "Note that there can be only one registered interpreter for each major Python release (e.g. 2.0, 2.1, 2,2 etc)" Both are 2.7 –  rudivonstaden Oct 2 '12 at 14:29
I was envisioning using it temporarily. ie. taking the C:\python version out of registry and using osgeo\python and then switching back later...best I could think of :) –  s_a Oct 2 '12 at 15:06
Please see add/remove the o4w python to/from Windows registry on osgeo4w website for an improved version of the effbot recipe, made specifically for o4w. –  matt wilkie Oct 2 '12 at 16:16
I should have looked there first! thanks mattwilkie –  s_a Oct 2 '12 at 16:17

I was able to get py2exe to work using a suggestion from wildintellect on IRC. After installing py2exe on the system python, I checked the install log (c:\Python27\py2exe-wininst.log) and copied the files into the o4w python installation (c:\OSGeo4W\apps\python27\lib). Basically, that meant the following files and directories:

C:\Python27\Lib\site-packages\py2exe-0.6.9-py2.7.egg-info
C:\Python27\Lib\site-packages\zipextimporter.py
C:\Python27\Lib\site-packages\_memimporter.pyd
C:\Python27\Lib\site-packages\py2exe


I also had to copy these DLLs into C:\OSGeo4W\apps\python27\DLLs to get it to link properly (basically I searched the c:\windows directory and copied across whatever caused it to fail):

msvsp90.dll
msvcr80.dll


There may be reasons why this is a bad idea, but if you are set on using py2exe, then this seems to work.

-
@rudivostaden: Would it be best to accept this answer so people know there is a solution? congratulations, btw,on solving your problem :) –  s_a Oct 2 '12 at 17:28
@s_a: I can only accept my own answer after 2 days... matt wilkie's solution might actually be the better option, but I suppose this answers the original question more explicitly. –  rudivonstaden Oct 2 '12 at 18:52
2 days...good to know. Also, no matter who gets the credit, at least we all have another osgeo4w problem solved! I am sure you are not the only one who wants to use py2exe. –  s_a Oct 2 '12 at 19:52