【问题标题】:Including compiled extension with cx_freeze包括使用 cx_freeze 编译的扩展
【发布时间】:2021-10-19 20:23:50
【问题描述】:

我有一个使用 scikit-build 构建的 C 扩展(因为它使用 CMake 库),并且在导入 python 时构建良好并且可以工作。

然后我尝试构建使用带有 cx-freeze 的扩展的脚本,该扩展正在构建中,但我看到“缺少模块”下列出的扩展:

Missing modules:
? _atscManufacturing imported from atsc.atscManufacturing.atscManufacturing

当我从命令提示符运行构建的可执行文件时,它只会挂起几秒钟,然后退出而没有输出。

我见过this solution,它建议使用Extension 添加C 扩展,但这似乎是要编译库而不是仅仅包括编译后的库。我认为这不会起作用,因为它需要构建 CMake 库。

这是我的主要setup.py

from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os", "atsc.atscManufacturing.atscManufacturing", "intelhex", "logging", "atexit", "sys"], "excludes": []}

base = None

setup(  name = "Manufacturing script",
    version = "1.0.0",
    description = "manufacturing script",
    options = {"build_exe": build_exe_options},
    executables = [Executable("main.py", base = base)])

这是./atsc 扩展中的setup.py

from skbuild import setup

setup(
    name="atscManufacturing",
    version="0.1",
    packages=['atscManufacturing']
)

【问题讨论】:

    标签: python windows cx-freeze scikit-build


    【解决方案1】:

    我能够让它工作,有几个问题:

    1. 我使用python setup.py build 构建atscManufacturing 模块。这会在构建目录中构建库。我添加了--inplace 选项以在模块中输出库。
    2. 该模块链接了一个作为共享库构建的 C 库。根据this issue,无法使用 scikit-build 复制已编译的共享库。可以使用自定义脚本将其从构建目录复制到模块目录中,但我只是将 C 库更改为静态构建,以便将其编译到 pyd 库中。这个问题令人困惑,因为我收到错误“python ImportError: DLL load failed while importing _atscManufacturing: The specified module could not be found.”这并不是说找不到 _atscManufacturing,而是说找不到 _atscManufacturing 所依赖的共享库。我通过在 Visual Studio 命令提示符下运行 dumpbin /dependents _atscManufacturing.cp310-win_amd64.pyd 来解决这个问题,其中列出了有问题的共享库
    3. 我使用 SWIG 生成包装器,由于某种原因,它生成的 python 文件在使用 python 运行时运行良好,但在从 cx-freeze exe 运行时失败(没有错误)。幸运的是我不需要那个,并在__init__.py 中加载了模块,所以我只是删除了该文件并更改了我在主 python 文件中的导入。没有错误使调试变得困难,我求助于添加 print 调用来查看脚本失败的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      相关资源
      最近更新 更多