smiddy84
smiddy84

Reputation: 305

Use Setuptools with C Extension as part of package

I wrote a C Extension to access an error message for a camera from a proprietary library. The structure is

setup.py
dicamsdk\
|---__init__.py
|---control.py
|---pcoError.c

with setup.py

from setuptools import setup, Extension, find_packages
from dicamsdk.control import __version__

pcoErrorModule = Extension("dicamsdk.pcoError",
                           sources=["dicamsdk/pcoError.c"],
                           include_dirs=['C:\Program Files (x86)'
                                         '\Digital Camera Toolbox'
                                         '\Sensicam SDK\include'],
                           define_macros=[("PCO_ERRT_H_CREATE_OBJECT", None)],
                           )
setup(
    name="pydicamsdk",
    platforms=["win-amd64", 'win32'],
    license="GNU GPLv3",
    ext_modules=[pcoErrorModule],
    packages=find_packages(),
    version=__version__
)

and the control.py intends to import the compiled C Extension with

from . import pcoError

When I try to build (or install) the package I always receive the error ImportError: cannot import name 'pcoError'.

The only way it seems to work is to comment out the import in control.py and build the C Extension with setup.py build_ext --inplace. Just with the compiled present I can build/install my library.

Is there a solution to be implemented in my setup.py to compile my extension in first place to enable a simple installation?

Upvotes: 5

Views: 2882

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114310

A more proper solution based on your own answer:

Your version is presumably for the entire project, not just the control module. It is standard to define __version__ in __init__.py. In that case, your import in setup.py would look like from dicamsdk import __version__. This will cause no conflicts, unless you do something foolish like import all your modules automatically from the package root.

Upvotes: 3

smiddy84
smiddy84

Reputation: 305

The problem was with the import at the beginning of setup.py. The import in line 2 (from dicamsdk.control import __version__) forced the code check of the module.

When removed, the install or build of the packages runs correctly.

Upvotes: 2

Related Questions