为 pip 配置 Python C 扩展

Configuring Python C extensions for pip

我写了一个 Python C 扩展,效果很好。通过 python setup.py install 安装有效。但是,pip 找不到我的头文件 - 因此 pip 安装不起作用。

> pip install
Collecting jcalg1==1.0.1
  Downloading https://files.pythonhosted.org/packages/a1/83/08b5fc2fbd36c8ac0668ae64c05cc88f3f6bd8fe68f058b19b11a463afa1/jcalg1-1.0.1.tar.gz
Installing collected packages: jcalg1
  Running setup.py install for jcalg1 ... error
    ERROR: Command errored out with exit status 1:
(...)
src\main.cpp(7): fatal error C1083: Cannot open include file: 'jcalg1.h': No such file or directory
(...)

这是我的 setup.py,头文件位于 src 文件夹中。

from setuptools import setup,Extension
import setuptools
from setuptools import find_packages
import pathlib

# The actual C extension
jc_module = Extension('jcalg1', include_dirs=["src"], sources = ['src\main.cpp'], libraries =["src\jcalg1_static"])

# The directory containing this file
HERE = pathlib.Path(__file__).parent

# The text of the README file
README = (HERE / "README.md").read_text()

# This call to setup() does all the work
setup(
    name="jcalg1",
    version="1.0.1",
    description="Interface to the JCALG1 compression library",
    long_description=README,
    long_description_content_type="text/markdown",
    url="https://github.com/CallMeAlexO/jcalg1",
    author="Alex Osheter",
    author_email="alex.osheter@gmail.com",
    license="MIT",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.7",
    ],
    ext_modules = [ jc_module ],
    packages=find_packages()
)

您可以在您的项目根目录中创建一个MANIFEST.in文件:

include src\*.h

include src\*.lib

然后重建你的包。它会将头文件和库文件添加到您的包中。