[英]setup.py - building c-extension with Numpy dependency
我創建了一個簡單的 c 函數(使用此處的指南Create Numpy ufunc ),我現在正嘗試在 pypi 上分發我的包。 為了讓它工作,它需要將 c 文件編譯成 .so 文件,然后可以從 python 導入,一切都很好。 要編譯它需要 Numpy 的頭文件numpy/ndarraytypes.h
。
在本地構建和安裝時,它工作正常。 這是因為我知道頭文件的位置。 但是在分發它時,我們在哪里可以找到 Numpy 文件夾? 從日志中可以明顯看出,在構建和安裝我的包之前安裝了 numpy,因此我只需要包含正確的 Numpy 文件夾。
from setuptools import setup
from setuptools import Extension
if __name__ == "__main__":
setup(
name="myPack",
install_requires=[
"numpy>=1.22.3", # <- where is this one located after it gets installed?
],
ext_modules=[
Extension(
'npufunc',
sources=['npufunc.c'],
include_dirs=[
# what to put here for it to find the "numpy/ndarraytypes.h" header file when installing?
"/usr/local/Cellar/numpy/1.22.3_1/lib/python3.9/site-packages/numpy/core/include/" # <- this one I have locally and when added, installtion works fine
]
),
]
)
您可以通過 python 從numpy
查詢包含目錄
import numpy
numpy.get_include()
這應該返回一個字符串( /usr/lib/python3.10/site-packages/numpy/core/include
在我的系統上),您可以將其添加到include_dirs
。 有關文檔,請參見此處。
至於您的問題: numpy
是您項目的build dependency
項。 舊的setup.py
方法在處理這些方面有點糟糕,這就是為什么它已被更現代的pyproject.toml
方法取代的原因,這(除其他外)使此類規范成為可能。 您的設置(setuptools + numpy)的一個相當小的pyproject.toml
看起來像這樣:
[build-system]
requires = ["setuptools", "wheel", "oldest-supported-numpy"]
build-backend = "setuptools.build_meta"
給定這樣一個pyproject.toml
,您可以通過調用使用 python 構建模塊構建擴展
python -m build
它應該產生一個帶有編譯的輪子。
檢查 package.json
大多數安裝的文件通常都在那里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.