Skip to main content
 首页 » 编程设计

python之ImportError:没有名为 Shell 的模块; shell 未导入(32 位,python)

2025年05月04日9wayfarer

我正在用 python 开发一个项目,其中包含许多不同的 python 文件和必须安装的额外库,我目前正在尝试编译一个 32 位版本(我们目前只有一个 64 位 .exe ).

然而,尽管在运行常规 python 文件时一切正常,但在运行编译后的 .exe(使用 py2exe 编译)时,我不断收到错误消息:

 Traceback (most recent call last): 
    File "fredchat.py", line 23, in <module> 
    File "fcio.pyc", line 20, in <module> 
 ImportError: No module named shell 

如预期的那样,fredchat.py 有一个

 import fcio 

第 23 行的命令。但是,在 fcio.py 中甚至没有提到“shell”这个词!

我们的 setup.py 非常基本:

 from distutils.core import setup 
 import py2exe 
 
 setup(console=['fredchat.py']) 

不幸的是,我无法提供任何实际代码,A. 因为它很长,B. 因为项目负责人对我们何时给出什么代码非常严格(可以理解)。

请您参考如下方法:

我发现这是 py2exe 和 win32com 的一个已知问题。 The py2exe wiki有一个解决方法。在 setup.py 文件的开头附近插入以下代码。这将诱使 py2exe 寻找 win32com.shell 模块并将其包含在分发中。

# ... 
# 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. 
# ...