I have a python package named pytools. It contains a cython-based submodule nms.
When I install the root package pytools with sudo python -H setup.py
,
the root package seems to be installed properly.
But the installation didn't copy compiled nms.so
to /usr/local/lib/python2.7/dist-packages/pytools/nms/
.
And When I import pytools in ipython, an error encountered:
ImportError: cannot import name nms
If I manually copy the pytools/nms/nms.so
to /usr/local/lib/python2.7/dist-packages/pytools/nms/
, the problem is solved.
Here is my setup.py
of the root package:
import os
import numpy
from distutils.core import setup, Extension
from Cython.Build import cythonize
exec(open('pytools/version.py').read())
exts = [Extension(name='nms',
sources=["_nms.pyx", "nms.c"],
include_dirs=[numpy.get_include()])
]
setup(name='pytools',
version=__version__,
description='python tools',
url='http://kaiz.xyz/pytools',
author_email='zhaok1206@gmail.com',
license='MIT',
packages=['pytools', 'pytools.nms'],
#packages=['pytools'],
zip_safe=False
)
And setup.py
of sub-package nms
:
from distutils.core import setup, Extension
import numpy
from Cython.Distutils import build_ext
setup(
cmdclass={'build_ext': build_ext},
ext_modules=[Extension("nms",
sources=["_nms.pyx", "nms.c"],
include_dirs=[numpy.get_include()])],
)
It seems that this is a duplicated question with Attempting to build a cython extension to a python package, not creating shared object (.so) file, but I still want to post it here because there is no much discussions there.
Thank you!
You don't need the setup script in a subpackage. Just build the extension in the root setup script:
Note that I wrap cythonized extension in
cythonize()
and use the full module name + full paths to extension sources. Also, sincenms
is a module inpytools
package, includingpytools.nms
inpackages
has no effect.