Win32, COM and the Installer

Gordon McMillan gmcm at
Thu Sep 21 03:27:06 CEST 2000

Greg Landrum wrote:

>[I realize that this is somewhat of a FAQ, but I have not been able
>to make the previously posted solutions work at all.  I would like
>to get a single answer posted that other people could track down.]

It's a FAQ without a simple answer...

>I'd like to construct a standalone windows program using Gordon
>McMillan's installer which uses the wonderful win32com stuff.  I'm
>running Win2K, Python 1.5.2, PythonWin build 132 and Installer 0.3f.
>Here's a very simple script I want to build an installer for, note
>that it imports win32com, but does not actually use it (to simplify
>the script):
>import win32api
>import sys,imp
># pythoncom and pywintypes are special, and require these hacks when
># built into a .EXE
>def magic_import(modulename, filename):
>  # win32 can find the DLL name.
>  h = win32api.LoadLibrary(filename)
>  found = win32api.GetModuleFileName(h)
>  # Python can load the module
>  mod = imp.load_module(modulename, None, found, ('.dll', 'rb',
>                                                  imp.C_EXTENSION))
>  # inject it into the global module list.
>  sys.modules[modulename] = mod
>  # And finally inject it into the namespace.
>  globals()[modulename] = mod
>  win32api.FreeLibrary(h)
>  import pywintypes
>except ImportError:
>  magic_import("pywintypes", "pywintypes15.dll")
>  import pythoncom
>except ImportError:
>  magic_import("pythoncom", "pythoncom15.dll")
>from win32com.client import gencache
>print 'hello world'
>The magic_import() bits here are from an earlier post by Mark Hammond.
>When I run the installer ( against this, I get the familiar
>IOError: [Errno 2] No such file or directory: 'c:\\windows\\system32

Simple, by default, will exclude pywintypes. Take that out (it's excluded 
because os.path.abspath will use the Win32 extensions if they're around, 
but has a sensible fallback if they're not).

Forget using gencache. You don't have to look far to see why - it's based 
on the assumption that win32com.client package is a bunch of .py files on 
your hardisk.

Mark tells me that you should be able to explicitly import the module that 
gencache would find for you, and Installer shouldn't have a problem with 
it. I've never tried that. I have gotten client.dynamic to work with the 
above tweaks.

>If someone can provide me with a .cfg file which works to create an
>installer/standalone for this script, I will be eternally grateful
>and will put together a HowTo in an effort to help fend off future

You write the HOWTO and I'll add it to Installer.

BTW, there's an in which has some 
fixes, but no (new) docs. One of these days I'll unbury myself sufficiently 
to make a proper distribution.

- Gordon

More information about the Python-list mailing list