建跨平臺Python Wheels:Windows/macOS/Linux全攻略)
如何使用Setuptools-rust構(gòu)建跨平臺Python WheelsWindows/macOS/Linux全攻略【免費(fèi)下載鏈接】setuptools-rustSetuptools plugin for Rust support項(xiàng)目地址: https://gitcode.com/gh_mirrors/se/setuptools-rustSetuptools-rust是一款強(qiáng)大的Setuptools插件專為Rust支持設(shè)計(jì)幫助開發(fā)者輕松構(gòu)建跨平臺的Python Wheels。本文將詳細(xì)介紹如何在Windows、macOS和Linux系統(tǒng)上使用Setuptools-rust構(gòu)建Python Wheels涵蓋從環(huán)境準(zhǔn)備到高級配置的完整流程。 環(huán)境準(zhǔn)備三平臺通用步驟在開始構(gòu)建之前需要確保系統(tǒng)中已安裝以下工具Python 3.7Rust 1.56pip 21.3setuptools 60.0安裝Setuptools-rust使用pip安裝Setuptools-rustpip install setuptools-rust克隆項(xiàng)目倉庫git clone https://gitcode.com/gh_mirrors/se/setuptools-rust cd setuptools-rust 基礎(chǔ)構(gòu)建快速生成WheelSetuptools-rust提供了簡單的命令來構(gòu)建Wheel適用于大多數(shù)場景。通用構(gòu)建命令在項(xiàng)目根目錄下執(zhí)行以下命令構(gòu)建Wheelpython -m build --wheel --no-isolation或者使用pippip wheel --no-deps . --wheel-dir dist構(gòu)建完成后Wheel文件將生成在dist目錄中。 平臺特定配置Windows平臺Windows平臺需要注意以下幾點(diǎn)工具鏈選擇Setuptools-rust默認(rèn)使用MSVC工具鏈但也支持GNU工具鏈。如果需要使用GNU工具鏈需在setup.py中明確指定。DLL文件處理在Windows上DLL文件在使用時(shí)會被系統(tǒng)鎖定。Setuptools-rust會自動處理DLL文件的復(fù)制和版本控制確保Wheel包中包含正確的依賴。激活虛擬環(huán)境.venv\Scripts\activatemacOS平臺macOS平臺支持構(gòu)建通用Wheeluniversal2同時(shí)支持x86_64和arm64架構(gòu)。構(gòu)建universal2 Wheel設(shè)置ARCHFLAGS環(huán)境變量ARCHFLAGS-arch x86_64 -arch arm64 python -m build --wheel --no-isolation激活虛擬環(huán)境source .venv/bin/activateLinux平臺Linux平臺推薦使用manylinux容器來構(gòu)建兼容多種Linux發(fā)行版的Wheel。使用manylinux容器首先拉取manylinux2014鏡像docker pull quay.io/pypa/manylinux2014_x86_64然后在容器中運(yùn)行構(gòu)建腳本docker run --rm -v pwd:/io quay.io/pypa/manylinux2014_x86_64 bash /io/build-wheels.sh構(gòu)建腳本示例項(xiàng)目中提供了構(gòu)建腳本示例如examples/html-py-ever/build-wheels.sh可根據(jù)需要進(jìn)行修改。 高級配置優(yōu)化Wheel構(gòu)建配置bdist_wheel在setup.cfg中添加[bdist_wheel]配置段可自定義Wheel構(gòu)建過程[bdist_wheel] py_limited_apicp37此配置啟用PEP 384穩(wěn)定API使Wheel支持多個(gè)Python版本。使用cibuildwheel[cibuildwheel]是一個(gè)強(qiáng)大的工具可通過GitHub Actions構(gòu)建多平臺Wheel。在項(xiàng)目中添加build-wheels.sh腳本并配置GitHub Actions工作流即可自動構(gòu)建Windows、macOS和Linux平臺的Wheel。處理生成文件如果Rust構(gòu)建過程中生成了需要包含在Wheel中的文件可使用RustExtension的generated_files選項(xiàng)RustExtension( my_package.my_module, Cargo.toml, generated_files[path/to/generated/file], ) 驗(yàn)證Wheel構(gòu)建完成后可使用以下命令驗(yàn)證Wheel內(nèi)容python -m zipfile -l dist/*.whl此命令將列出Wheel包中包含的所有文件確保沒有遺漏必要的組件。 參考文檔官方文檔docs/building_wheels.md示例項(xiàng)目examples/Setuptools-rust源碼setuptools_rust/通過本文介紹的方法您可以輕松使用Setuptools-rust在Windows、macOS和Linux平臺上構(gòu)建高質(zhì)量的Python Wheels為您的項(xiàng)目提供更好的分發(fā)和安裝體驗(yàn)。【免費(fèi)下載鏈接】setuptools-rustSetuptools plugin for Rust support項(xiàng)目地址: https://gitcode.com/gh_mirrors/se/setuptools-rust創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考