Reputation: 323
My code is:
from win32com.shell import shellcon
from win32com.shell.shell import ShellExecuteEx
And it works fine in IDLE but after i make the exe i get the error:
File "Myfile.py", line 1, in <module>
ImportError: No module named shell
Why can't py2exe import win32com.shell
?
Upvotes: 1
Views: 3514
Reputation: 477
The following may help you out: py2exe.org win32com.shell
The link describes the problem as being that win32com performs some "magic" to allow loading of COM extensions during run time. The extensions reside in the win32comext directory in site-packages and cannot be loaded directly. The __path__ variable for win32com gets modified to point to both win32com and win32comext. This run time change to __path__ trips up the modulefinder for py2exe so it must be told ahead of time.
The following is the code, supposedly from the source code for SpamBayes which dealt with the same issue, so a similar approach may work for you:
# ...
# ModuleFinder can't handle runtime changes to __path__, but win32com uses them
try:
# py2exe 0.6.4 introduced a replacement modulefinder.
# This means we have to add package paths there, not to the built-in
# one. If this new modulefinder gets integrated into Python, then
# we might be able to revert this some day.
# if this doesn't work, try import modulefinder
try:
import py2exe.mf as modulefinder
except ImportError:
import modulefinder
import win32com, sys
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
from distutils.core import setup
import py2exe
# The rest of the setup file.
Upvotes: 4