簡體   English   中英

Cython 嵌入在 Windows 上

[英]Cython embed on Windows

我已閱讀How to enable `--embed` with cythonize? Cython --embed 標志在 setup.py但此方法不適用於 Windows。 假設我們有:

# app.py
print("hello")
# build.py
import setuptools   # allows us to avoid calling vcvarsall.bat, see https://stackoverflow.com/a/53172602/
from distutils.core import setup
from Cython.Build import cythonize
from Cython.Compiler import Options
Options.embed = "main"
setup(ext_modules=cythonize(r'app.py', build_dir="release"), script_args=['build'], options={'build': {'build_lib': 'release'}})

在適用於 Windows 的 Python 3.8 上運行此build.py腳本不會像使用命令行命令那樣生成app.exe文件:

cython app.py --embed

相反,它會生成一個.pyd文件。

如何在 Windows 上使用 Python 腳本中的cythonize + embed生成 .exe?

已解決:實際上問題不是來自cythonize本身,而是來自事實distutils.core.setup(...)被配置為編譯+鏈接為 .pyd 而不是 .exe。

這是解決方案:

from distutils._msvccompiler import MSVCCompiler    # "from distutils.msvccompiler" did not work for me!
from Cython.Compiler import Options
Options.embed = "main"
cythonize(r'src\app.py', build_dir="build")

compiler = MSVCCompiler()
compiler.compile([r"build\src\app.c"], include_dirs=["C:/Python38/include"])
compiler.link_executable([r"build\src\app.obj"], "app", libraries=["python38"], library_dirs=["C:/Python38/libs"], output_dir="release", extra_preargs=["/NOIMPLIB", "/NOEXP"])

.exe 將位於release文件夾中。

(注意:我還將 Cython 升級到最新版本 0.29.30,它可能也有幫助)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM