python - Py2exe导入错误: No module named shell

标签 python import executable py2exe

我的代码是:

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/

相关文章:

linux - 如何获得二进制文本部分的偏移量和大小?

c - 如何使 winapi 中的文件不可执行?

python - 运行 pip/easy_install 时需要 sudo 吗?

python - 将 YAML 文件中的数据加载到 python 中的变量中

java - Eclipse导入莫名其妙没有解决

csv - 有没有免费的工具可以将超过 65000 个寄存器的文件从 DBF 格式转换为 CSV?

python - 聚类Python源代码

python - 使用 yum 安装软件包到 alt python 位置

c# - 我如何在c#中使用c++ dll

java - 方法返回后做一些事情