简体   繁体   English

使用cx_freeze制作exe时出现EnsureDispatch错误

[英]EnsureDispatch error when using cx_freeze for making exe

I am working with Python 3.4 on Windows 7. My setup file is as follows: 我正在Windows 7上使用Python 3.4。我的安装文件如下:

from cx_Freeze import setup, Executable, sys

exe=Executable(
 script="XYZ.py",
 base="Win32Gui",

 )
includefiles=[]
includes=[]
excludes=[]
packages=[]
setup(

 version = "1.0",
 description = "XYZ",
 author = "MAX",
 name = "AT",
 options = {'build_exe': {'excludes':excludes,'packages':packages,'include_files':includefiles}},
 executables = [exe]
 )


from distutils.core import setup
import py2exe, sys, os, difflib

sys.argv.append('py2exe')
setup(
    options = {'py2exe': {'bundle_files': 1}},
    console = [{'script': "XYZ.py"}],
    zipfile = None,
    )

When the obtained exe is run, an error pops up saying: 运行获得的exe时,弹出错误提示:

...
File "C:\Python34\Lib\site-packages\win32com\client\CLSIDToClass.py", line 46, in GetClass
    return mapCLSIDToClass[clsid]
KeyError: '{00020970-0000-0000-C000-000000000046}'

I just can't figure out the problem here. 我只是无法在这里解决问题。 Help, please. 请帮助。

Thanks. 谢谢。

You are using static proxy which is generated on your disk and which has the compiled executable trouble finding. 您正在使用在磁盘上生成的静态代理,该静态代理具有编译后的可执行文件查找问题。 If you do not know what the static proxy is, you are probably using win32com.client.gencache.EnsureDispatch which generates static proxy automatically. 如果您不知道静态代理是什么,则可能使用的是win32com.client.gencache.EnsureDispatch ,它会自动生成静态代理。

The easiest way to fix the problem is to use dynamic proxy by using win32com.client.dynamic.Dispatch . 解决此问题的最简单方法是使用win32com.client.dynamic.Dispatch使用动态代理。 Static proxy has some benefits, but there is a high possibility that you do not need it. 静态代理有一些好处,但是您很有可能不需要它。

You can find more information about static and dynamic proxies to COM objects here: http://timgolden.me.uk/python/win32_how_do_i/generate-a-static-com-proxy.html 您可以在此处找到有关COM对象的静态和动态代理的更多信息: http : //timgolden.me.uk/python/win32_how_do_i/generate-a-static-com-proxy.html

I've just figured out that the problem with EnsureDispatch is within gencache module, it assumes to be in read-only mode when an executable is built with cx_freeze . 我刚刚发现, EnsureDispatch的问题在gencache模块内,当使用cx_freeze构建可执行文件时,它假定处于只读模式。

The following lines allow cache to be built inside AppData\\Local\\Temp\\gen_py\\#.#\\ directory in Windows 7 x64: 以下几行允许在Windows 7 x64的AppData\\Local\\Temp\\gen_py\\#.#\\目录中构建缓存:

from win32com.client import gencache
if gencache.is_readonly:
    gencache.is_readonly = False
    gencache.Rebuild() #create gen_py folder if needed

References: 参考文献:

PS Performance is much better with static dispatch 使用静态调度,PS性能要好得多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM