【问题标题】:How to enforce ealry-binding with cx_freeze and win32com?如何使用 cx_freeze 和 win32com 强制绑定?
【发布时间】:2013-11-28 17:15:31
【问题描述】:

我制作了一些 python 脚本来控制外部 CATIA 应用程序。我现在必须将这些脚本打包成可执行文件,但我无法做到。

问题:

一旦构建脚本,如何强制 win32com 对特定模块使用早期绑定?


详情:

我的脚本使用 win32com.client 模块控制 CATIA 应用程序。我处理具有后期绑定的 CATIA,除了模块 CATIA V5 SpaceAnalysisInterfaces 对象库 包含具有参考输入/输出参数的函数。对于这个我使用早期绑定,遗憾的是,MakePy 的简单使用还不够,我必须修改 win32com.gen_py 包中生成的源以获得正确的行为从输入/输出参数。现在当我执行 python 脚本时它工作正常。但是,如果我使用 py2execx_freeze 构建它们,则可执行文件仅使用后期绑定,因此我得到了不好的结果。

这是我挂钩 CATIA 应用程序并使用其 API 的方式:

import win32com.client

buff = [0, 0, 0]

catApp = win32com.client.GetActiveObject("CATIA.Application") # Late bind needed
doc = catApp .Documents.Open(path)
part = doc.Part # This property fails if using early binding
spa = doc.GetWorkbench(u"SPAWorkbench")

I = spa.Inertias.Add(part) # Early bind needed

cogCoords = I.GetCOGPosition(buff) # The damn input/ouput argument function

这是我使用 cx_freeze 的构建脚本:

from cx_Freeze import setup, Executable

options = {
    "includes": [],
    "excludes": [],
    "packages": ["win32com.gen_py"]
    }

target = Executable(
    script = "test.py",
    base = "Console",
    compress = True,
    icon = None,
    )

setup(
    name = "Test",
    version = "1.0",
    description = "Early Binding Test Built",
    author = "C.LECLERC",
    options = {"build_exe": options},
    executables = [target]
    )

这个构建脚本会生成一堆文件,包括我实际的 win32com.gen_py 模块的内容,所以它应该可以工作。但是当我执行文件时,它只使用后期绑定。当我使用 py2exe 时,我无法检查文件是否正确添加,但行为完全相同:late bind !


我查看了this post,但我的问题有所不同。模块被正确复制并且脚本不会引发异常。输入/输出功能无法正常工作。

任何帮助将不胜感激。

【问题讨论】:

    标签: python win32com cx-freeze late-binding early-binding


    【解决方案1】:

    我回答我自己的问题以分享我找到的解决方案并将问题标记为已解决。

    解决办法:

    只需将 'dicts.dat' 文件从 win32com.gen_py 包复制到 'library.zip' 的等效文件夹中cx_freeze 生成的存档。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2014-07-05
    • 2011-04-14
    • 2011-08-14
    • 2011-08-06
    • 2013-03-07
    • 2019-08-11
    相关资源
    最近更新 更多