[Tutor] Problem with converting Python to EXE using py2exe

Dave Angel davea at ieee.org
Sat Apr 11 21:07:38 CEST 2009

Alan Gould wrote:
> <snip...>
> Good idea, I forgot that in Python you can find the full path that way.
> Too used to working in C/C++ where the file macro only gives the
> filename...
In C++, you wouldn't want the full path to the source file, but the full 
path to the .EXE file.  That can be gotten in Windows, by using 
GetModuleHandle(0), and some other function on that handle.  Sorry I 
don't remember the name of the second function, but I haven't done any 
C++ work in a few years.  It might have a name like 
GetModuleXXXXX(handle).  Anyway, that's the technique I used in that 
environment.  To me, if it's possible to avoid an install entirely, I'm 
in favor.

Two other tricks I used in the C++/Win world; 

1) Let the name of the exe, as well as the location, be significant.  If 
you put most of the code in dll's, it doesn't cost much to have multiple 
copies of the .EXE, each one with a different behavior.  That's 
especially useful when you make file associations or SendTo associations 
to those various executables.  Windows launches the program with only 
the filename as an argument, but I can have one more parameter hidden in 
the filename.

2) You can append arbitrary data to the EXE, and it won't affect normal 
running.  Windows only maps in the amount specified in the header.  But 
you can fetch readonly data from the end of the file, and use it any way 
you like.   To append, you can simply do    COPY /B  myprog.exe + 

I wish Python.exe was designed this way.  We could package up small 
scripts by appending them to a renamed EXE,   The user could treat that 
as a self-contained executable, ready to run.  This wouldn't obviate the 
need for installing Python itself, but it could make integrating it into 
normal usage simpler.

See EXEMAKER for an implementation of #1, though it doesn't work with 
some imports, and has the problem that a console gets created even if 
you're running a GUI python script.  It's a good start, though.

More information about the Tutor mailing list