How To Put My Python C-module Inside Package?
Solution 1:
There must be an __init__.py file in the foopkg folder, otherwise Python does not recognize as a package.
Create a foopkg folder where setup.py is, and put an empty file __init__.py there, and add a packages line to setup.py:
from distutils.coreimport setup
from distutils.extensionimportExtensionsetup(name='foobar',
version='1.0',
packages=['foopkg'],
ext_modules=[Extension('foopkg.foo', ['foo.c'])],
)
Solution 2:
distutils will be deprecated since python 3.10 , instead you can use setuptools, which is an enhanced alternative of distutils , so you don't need to pass packages argument in your setup(). For example :
from setuptools import setup, Extension
setup(name='foobar',
version='1.0',
ext_modules=[Extension('foopkg.foo', ['foo.c'])],
)
Then build and install your C extension
python /PATH/TO/setup.py install
After building your C extension successfully, test whether it can be imported as expected by running this command:
python -c "from foopkg import foo"[side note]
One more thing about setuptool is that you can uninstall the C extension package by simply running pip uninstall, for example : python -m pip uninstall foobar
Post a Comment for "How To Put My Python C-module Inside Package?"