简体   繁体   中英

Compile Python C Extensions with cx_Freeze

I am trying to create an exe on Windows from a Python 3 package with a C extension module. In distutils, you can create an extension like this:

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])

Than, the extension will be compiled with the appropriate compiler and placed alongside your other modules with the command:

python setup.py build_ext --inplace

cx_Freeze is a module that can package your code into an exe file along with a Python interpreter and the relevant packages. Then, an end user can use your program without having a Python installation. Unfortunately, cx_Freeze doesn't have an Extension class, and I cannot find a way to handle compilation with cx_Freeze.

One solution I am unsure about is to first build the extensions in place with distutils/setuptools, and then use cx_Freeze to create the executable. I don't want to reinvent the wheel though, so I wonder if someone else with more experience in this area has a solution.

I found a working solution. I can import Extension from distutils, and pass it into the setup from cx_Freeze:

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

This makes sense, since cx_Freeze is built on top of distutils. Originally, I was trying to use setuptools.setup , but that doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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