【发布时间】:2021-05-25 19:15:03
【问题描述】:
msgpack 包括一个可选的 cython 延期。 Some users of the package want py3-none-any wheels of msgpack。我想弄清楚如何制作 可以使用和不使用可选扩展来构建轮子。
【问题讨论】:
标签: python cython setuptools python-wheel python-extensions
msgpack 包括一个可选的 cython 延期。 Some users of the package want py3-none-any wheels of msgpack。我想弄清楚如何制作 可以使用和不使用可选扩展来构建轮子。
【问题讨论】:
标签: python cython setuptools python-wheel python-extensions
一种可能的解决方案是使用setup.py 中的环境变量来决定
是否将ext_modules设置为setuptools.Extension列表的空列表
[build-system]
requires = ["setuptools", "wheel", "cython"]
build-backend = "setuptools.build_meta"
from setuptools import setup, Extension
import os
if 'ONLY_PURE' in os.environ:
ext_modules = []
else:
module1 = Extension('helloworld', sources = ['helloworld.pyx'])
ext_modules = [module1]
setup(ext_modules=ext_modules)
[metadata]
name = mypackage
version = 0.0.1
[options]
py_modules = mypackage
try:
import helloworld
except ImportError:
print('hello pure python')
print("hello extension")
$ pip install build
...
$ python -m build
...
$ ls dist/
mypackage-0.0.1-cp39-cp39-linux_x86_64.whl mypackage-0.0.1.tar.gz
$ pip install build
...
$ ONLY_PURE='a nonempty string' python -m build
...
$ ls dist/
mypackage-0.0.1-py3-none-any.whl mypackage-0.0.1.tar.gz
【讨论】:
pip install mypackage-0.0.1-py3-none-any.whl 的结果是什么?我不明白pip 是如何安装需要扩展而没有所述扩展的轮子。
pip install mypackage-0.0.1-py3-none-any.whl。 pip 只安装 mypackage.py 因为(与 mypackage-0.0.1-cp39-cp39-linux_x86_64.whl 不同)mypackage-0.0.1-py3-none-any.whl 不包含 可选 helloworld 扩展。当mypackage.py 尝试导入helloworld 时,ImportError 被抛出,mypackage.py 回退到打印'hello pure python'