【问题标题】:ImportError: No Module Named Shell; shell not imported (32-bit, python)ImportError: 没有名为 Shell 的模块;未导入 shell(32 位,python)
【发布时间】:2012-08-23 03:35:32
【问题描述】:

我正在使用 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. 因为项目负责人对我们何时提供什么代码非常严格(可以理解)。

【问题讨论】:

  • 不应该是import fcio而不是import fcio.py吗?
  • 呃,是的。谢谢你,我为那个愚蠢的错误道歉。 >.
  • 您的 .pyc 文件是从 64 位 Python 生成的吗?尝试删除所有 .pyc 文件,然后再次运行。
  • fcio 的第 20 行完全空白;我尝试删除 .pyc 文件,但仍然有问题。有没有办法强制它在 py2exe setup.py 中包含 shell 模块?

标签: python shell 32-bit py2exe importerror


【解决方案1】:

我发现这是 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.
# ...

【讨论】:

    【解决方案2】:

    可能是动态导入了有问题的模块,而 py2exe 在组装依赖项时无法识别它。您可以尝试直接在代码中导入该模块并创建一个新的可执行文件。其他可能性是强制 py2exe 包含模块,将以下内容添加到您的配置中:

    opts = {
      'py2exe': { "includes" : ["qualified_module_name"] }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 1970-01-01
      • 2018-09-02
      • 2018-02-08
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 2020-11-28
      相关资源
      最近更新 更多