Size: 2333
Comment: Added comment to code for handling 0.6.4
|
Size: 3030
Comment: fix links
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
## page was renamed from WinShell | |
Line 3: | Line 4: |
I used [http://tgolden.sc.sabren.com/python/winshell.html winshell] from Tim Golden - a thin wrapper around Windows Shell-Functions. | `py2exe` doesn't find imported `win32com.shell` module, and creates invalid distribution without this file. As a result, an application that uses this module may crash at random or miss some features. |
Line 5: | Line 6: |
winshell.py starts with | For example version 0.2 of [[http://tgolden.sc.sabren.com/python/winshell.html|winshell.py]] from Tim Golden (a thin wrapper around Windows Shell-Functions) starts with: |
Line 12: | Line 13: |
py2exe learns, that something is missing: |
py2exe reports that something is missing: |
Line 19: | Line 19: |
and starting the programm leaves: | and starting the program leaves: |
Line 33: | Line 33: |
win32com does some magic. There really is no win32com.shell anywhere. | `win32com` does some magic in order to allow loading of COM extensions during run time. The actual extensions reside in the `win32comext` directory under site-packages and can't be loaded directly. `win32com`'s [[http://docs.python.org/tutorial/modules.html#packages-in-multiple-directories|__path__]] variable has been changed to point to both `win32com` and `win32comext`. `py2exe`'s modulefinder can't handle runtime changes in `__path__` so we have to tell it about the change beforehand. |
Line 37: | Line 37: |
Without shame I browsed [http://cvs.sourceforge.net/viewcvs.py/spambayes/ spambayes source code] and found: {{{ #!python # ModuleFinder can't handle runtime changes to __path__, but win32com uses them, # particularly for people who build from sources. Hook this in. try: import modulefinder # in 0.6.4 use "import py2exe.mf as modulefinder" import win32com for p in win32com.__path__[1:]: modulefinder.AddPackagePath("win32com", p) for extra in ["win32com.shell","win32com.mapi"]: __import__(extra) m = sys.modules[extra] for p in m.__path__[1:]: modulefinder.AddPackagePath(extra, p) except ImportError: # no build path setup, no worries. pass }}} I just added that in top of my setup.py file: |
Without shame I browsed [[http://spambayes.svn.sourceforge.net/viewvc/spambayes/trunk/spambayes/|Spambayes source code]] and found the code to fix it. |
Line 70: | Line 49: |
import modulefinder # in 0.6.4 use "import py2exe.mf as modulefinder" | # if this doesn't work, try import modulefinder import py2exe.mf as modulefinder |
Line 85: | Line 65: |
# ... # The rest of the setup file. # ... |
|
Line 88: | Line 72: |
= Impact = The following products included workaround: * Spambayes * Mercurial * hgsvn = Ideal proposal = `py2exe` should detect such well-known problems with well-known modules, and when `win32com.shell` is missing, a workaround should be automatically applied with a corresponding log message: {{{ The following modules appear to be missing ['Interface', 'intSet', 'mxDateTime.__version__', 'win32com.shell'] Applying automatic workaround for known problem with win32com.shell }}} `py2exe` should also fail if required modules are missing unless some `--force` key in effect. |
The Problem
py2exe doesn't find imported win32com.shell module, and creates invalid distribution without this file. As a result, an application that uses this module may crash at random or miss some features.
For example version 0.2 of winshell.py from Tim Golden (a thin wrapper around Windows Shell-Functions) starts with:
py2exe reports that something is missing:
The following modules appear to be missing ['Interface', 'intSet', 'mxDateTime.__version__', 'win32com.shell']
and starting the program leaves:
Traceback (most recent call last): File "xxxxxx.py", line 33, in ? File "xxxxxxx.pyo", line 9, in ? File "winshell.pyo", line 27, in ? ImportError: No module named shell
in the log.file.
Explanation
win32com does some magic in order to allow loading of COM extensions during run time. The actual extensions reside in the win32comext directory under site-packages and can't be loaded directly. win32com's __path__ variable has been changed to point to both win32com and win32comext. py2exe's modulefinder can't handle runtime changes in __path__ so we have to tell it about the change beforehand.
Solution
Without shame I browsed Spambayes source code and found the code to fix it.
1 # By default, the installer will be created as dist\Output\setup.exe.
2
3 import time
4 import sys
5
6 # ModuleFinder can't handle runtime changes to __path__, but win32com uses them
7
8 try:
9 # if this doesn't work, try import modulefinder
10 import py2exe.mf as modulefinder
11 import win32com
12 for p in win32com.__path__[1:]:
13 modulefinder.AddPackagePath("win32com", p)
14 for extra in ["win32com.shell"]: #,"win32com.mapi"
15 __import__(extra)
16 m = sys.modules[extra]
17 for p in m.__path__[1:]:
18 modulefinder.AddPackagePath(extra, p)
19 except ImportError:
20 # no build path setup, no worries.
21 pass
22
23 from distutils.core import setup
24 import py2exe
25
26 # ...
27 # The rest of the setup file.
28 # ...
And this worked.
Impact
The following products included workaround: * Spambayes * Mercurial * hgsvn
Ideal proposal
py2exe should detect such well-known problems with well-known modules, and when win32com.shell is missing, a workaround should be automatically applied with a corresponding log message:
The following modules appear to be missing ['Interface', 'intSet', 'mxDateTime.__version__', 'win32com.shell'] Applying automatic workaround for known problem with win32com.shell
py2exe should also fail if required modules are missing unless some --force key in effect.