Search code examples
pythonsetuptoolscpythonpython-c-api

Configuring Python C extensions for pip


I wrote a Python C extension, and it works great. Installing via python setup.py install works. However, pip cannot find my header files - so a pip installation doesn't work.

> 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
(...)

This is my setup.py, the header file is located in the src folder.

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="[email protected]",
    license="MIT",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.7",
    ],
    ext_modules = [ jc_module ],
    packages=find_packages()
)

Solution

  • You can create a MANIFEST.in file in your project root directory:

    include src\*.h
    
    include src\*.lib
    

    Then rebuild your package. It will add the header files and library files into your package.