py2exe: "ImportError: No module named pywintypes" ...?? :-(

Thomas Heller theller at
Thu Aug 7 14:32:36 CEST 2003

[posted and mailed]
steinhardt at (x-herbert) writes:

(Herbert, athough you have also sent mail privately to me I prefer to
answer on the newsgroup, just for the archives)

> Hi,
> I have a small test to "compile" al litle script as a WMI-Tester. The
> script include a wmi-wrapper and "insert" the Win32-modeles.
> here the code:
> my ""
> -----
> import wmi
> computer = wmi.WMI () 
> for item in computer.Win32_Process (): 
>   print item
> -----
Ok. After installing and rebooting my machine, this works for me
when run from Python.

> the
> -----
> from distutils.core import setup
> setup(name="WMI_tester", scripts=[""],)
> -----
This script is not complete (but certainly a cut and paste error), you
have to add 'import py2exe'.

> and the error at 
> " py2exe":
> Traceback (most recent call last):
>   File "C:\Programme\ActivePython22\Lib\site-packages\Pythonwin\pywin\framework\",
> line 301, in RunScript
>     exec codeObject in __main__.__dict__
>   File "C:\Dokumente und Einstellungen\steinha\Eigene
> Dateien\WinProcess\PythonProc\", line 7, in ?
>     setup(name="WMI_tester", scripts=[""],)
>   File "C:\Programme\ActivePython22\lib\distutils\", line 138,
> in setup
>     dist.run_commands()
>   File "C:\Programme\ActivePython22\lib\distutils\", line 893,
> in run_commands
>     self.run_command(cmd)
>   File "C:\Programme\ActivePython22\lib\distutils\", line 913,
> in run_command
>   File "C:\Programme\ActivePython22\Lib\site-packages\py2exe\",
> line 604, in run
>     mf.import_hook(f)
>   File "C:\Programme\ActivePython22\Lib\site-packages\py2exe\tools\",
> line 126, in import_hook
>     q, tail = self.find_head_package(parent, name)
>   File "C:\Programme\ActivePython22\Lib\site-packages\py2exe\tools\",
> line 180, in find_head_package
>     raise ImportError, "No module named " + qname
> ImportError: No module named pywintypes

It seems that either your installation is corrupted or py2exe doesn't work with
ActiveState Python any longer, py2exe 0.4.1 requires a fairly new build
of the win32 extensions (154 or later, IIRC). Mark changed the way the special
pywintypes and pythoncom extensions are imported.
For me (Python 2.2.2, and a recent win32all) the py2exe build process
runs successfully.

The deeper problem occurs when the executable is run:

  Traceback (most recent call last):
    File "<string>", line 1, in ?
    File "imputil.pyc", line 103, in _import_hook
    File "<string>", line 52, in _import_top_module
    File "imputil.pyc", line 216, in import_top
    File "imputil.pyc", line 271, in _import_one
    File "<string>", line 128, in _process_result
    File "wmi.pyc", line 117, in ?
    File "win32com\client\gencache.pyc", line 435, in EnsureDispatch
    File "win32com\client\gencache.pyc", line 419, in EnsureModule
    File "win32com\client\gencache.pyc", line 236, in MakeModuleForTypelib
    File "win32com\client\makepy.pyc", line 273, in GenerateFromTypeLibSpec
    File "win32com\client\gencache.pyc", line 449, in AddModuleToCache
    File "win32com\client\gencache.pyc", line 473, in _GetModule
    File "imputil.pyc", line 132, in _import_hook
    File "<string>", line 70, in _finish_import
    File "imputil.pyc", line 318, in _load_tail
  ImportError: No module named win32com.gen_py

That is because the wmi module uses early binding to get some COM
constants described in a type library. For this, one would need to use
the --progids option to py2exe, so that the win32com.gen_py directory
which contains the makepy output is included into the exe.

Unfortunately this only works for *modules* in this directory, and not
(yet?) for packages, but wmi requires/creates a package named
565783C6-CB41-11D1-8B02-00600806D9B6x0x1x2 here, and I cannot come up
with a quick hack right now, neither do I know how to guess this name.

And I don't even find docs for win32com.client.GetObject("winmgmts:").
How can I, from this call, find out the typelib guid and version?

Also, I'm not sure if it is a good idea to include the makepy generated
modules into the executable. Maybe it would be better to create them at
runtime in a subdirectory (and include the makepy module into the exe).

Can anyone help?


More information about the Python-list mailing list