Py2exe ImportError: No module named shell

耗尽温柔 提交于 2019-12-24 14:08:26

问题


My code is:

from win32com.shell import shellcon
from win32com.shell.shell import ShellExecuteEx

And it works fine in IDLE but after i make the exe i get the error:

File "Myfile.py", line 1, in <module>
ImportError: No module named shell

Why can't py2exe import win32com.shell?


回答1:


The following may help you out: py2exe.org win32com.shell

The link describes the problem as being that win32com performs some "magic" to allow loading of COM extensions during run time. The extensions reside in the win32comext directory in site-packages and cannot be loaded directly. The __path__ variable for win32com gets modified to point to both win32com and win32comext. This run time change to __path__ trips up the modulefinder for py2exe so it must be told ahead of time.

The following is the code, supposedly from the source code for SpamBayes which dealt with the same issue, so a similar approach may work for you:

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


来源:https://stackoverflow.com/questions/31459325/py2exe-importerror-no-module-named-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!