Skip to main content
 首页 » 编程设计

python之py2exe 可执行文件生成日志文件错误

2025年05月04日7bluestorm

Py2exe 无一异常(exception)地构建可执行文件。当我运行可执行文件时,会生成一个日志文件,内容如下:

    Traceback (most recent call last): 
       File "ecm2es_gui.py", line 10, in <module> 
       File "weblogin.pyo", line 4, in <module> 
       File "mechanize\__init__.pyo", line 122, in <module> 
       File "mechanize\_mechanize.pyo", line 14, in <module> 
       File "mechanize\_html.pyo", line 19, in <module> 
       File "mechanize\_form.pyo", line 64, in <module> 
    ImportError: No module named inspect 

当我从 Python Shell、Eclipse 或 Geany 运行该程序时,我没有收到任何错误并且运行正常。

我原以为我的问题出在 Mechanize 或 eggs 的安装上,但现在我认为这不是问题所在。

有什么想法吗? TIA - 布拉德

更新...这是我的 setup.py 文件:

    from distutils.core import setup 
    import py2exe 
 
    import sys; sys.argv.append('py2exe') 
 
    includes = [] 
    excludes = ['_ssl', 'pdb', 'unittest', 'inspect', 
                'pyreadline', 'difflib', 'doctest', 'locale', 
                'optparse', 'pickle', 'calendar', '_gtkagg',  
                '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger', 
                'pywin.debugger.dbgcon', 'pywin.dialogs'] 
    packages = [] 
    dll_excludes = [] 
 
    setup( 
        options = {"py2exe": {"compressed": 1, 
                              "optimize": 2, 
                              "bundle_files": 3, 
                              "includes": includes, 
                              "excludes": excludes, 
                              "packages": packages, 
                              "dll_excludes": dll_excludes, 
                              "dist_dir": "dist", 
                              "xref": False, 
                              "skip_archive": False, 
                              "ascii": False, 
                              "custom_boot_script": '', 
                             } 
                  }, 
    #    zipfile = None, 
        name='EnerSave Uploader', 
        version='0.5', 
        description='Upload ECM-1240 Data to EnerSave', 
        author='Brad Norman', 
        windows=[{"script":"ecm2es_gui.py",  
                  "icon_resources": [(1, "favicon.ico")]} 
                ] 
        ) 

请您参考如下方法:

错误是

ImportError: No module named inspect 

并且在您的设置脚本中,您在排除列表中有检查。从排除项中删除它,py2exe 会将它与您的可执行文件打包在一起,以便 mechanize 可以使用它。