Py2exe ImportError: No module named shell

Py2exe ImportError: No module named shell

我的代码是:

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

link 将问题描述为 win32com 执行一些 "magic" 以允许在 运行 期间加载 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.