【问题标题】:Compile Python C Extensions with cx_Freeze使用 cx_Freeze 编译 Python C 扩展
【发布时间】:2013-09-24 00:12:27
【问题描述】:

我正在尝试从带有 C 扩展模块的 Python 3 包在 Windows 上创建一个 exe。在 distutils 中,你可以像这样创建一个扩展:

from distutils.core import setup, Extension

module1 = Extension('demo',
                     sources = ['demo.c'])

setup (name = 'PackageName',
       version = '1.0',
       description = 'This is a demo package',
       ext_modules = [module1])

然后,扩展将使用适当的编译器进行编译,并使用以下命令放置在您的其他模块旁边:

python setup.py build_ext --inplace

cx_Freeze 是一个模块,它可以将您的代码与 Python 解释器和相关包一起打包成 exe 文件。然后,最终用户无需安装 Python 即可使用您的程序。不幸的是,cx_Freeze 没有扩展类,我找不到使用 cx_Freeze 处理编译的方法。

我不确定的一个解决方案是首先使用 distutils/setuptools 构建扩展,然后使用 cx_Freeze 创建可执行文件。不过我不想重新发明轮子,所以我想知道在这方面有更多经验的其他人是否有解决方案。

【问题讨论】:

    标签: python windows distutils cx-freeze


    【解决方案1】:

    我找到了一个可行的解决方案。我可以从 distutils 导入 Extension,然后从 cx_Freeze 将其传递到设置中:

    from cx_Freeze import setup
    from distutils.core import Extension
    ...
    setup=(...
           ext_modules=Extension(...))
    

    这是有道理的,因为 cx_Freeze 是建立在 distutils 之上的。最初,我试图使用setuptools.setup,但这不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-08
      • 2011-07-04
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2014-08-18
      相关资源
      最近更新 更多