我的代码是:
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 扩展。扩展驻留在站点包中的 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.
关于python - Py2exe导入错误: No module named shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31459325/