我的代码是:
from win32com.shell import shellcon
from win32com.shell.shell import ShellExecuteEx
它在 IDLE 中工作正常,但在我生成 exe 后出现错误:
File "Myfile.py", line 1, in <module>
ImportError: No module named shell
为什么py2exe无法导入win32com.shell
?
以下内容可能会帮助您:py2exe.org win32com.shell
该链接将问题描述为 win32com 执行了一些“魔法”以允许在运行时加载 COM 扩展。这些扩展位于 site-packages 的 win32comext 目录中,无法直接加载。 win32com 的 __path__ 变量被修改为同时指向 win32com 和 win32comext。对 __path__ 的运行时更改会导致 py2exe 的模块查找器出错,因此必须提前告知。
以下是代码,据说来自处理相同问题的 SpamBayes 源代码,因此类似的方法可能适合您:
# ...
# 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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)