python setuptools: how can I install package with

2019-07-26 16:28发布

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!

1条回答
Evening l夕情丶
2楼-- · 2019-07-26 16:51

You don't need the setup script in a subpackage. Just build the extension in the root setup script:

exts = [Extension(name='pytools.nms',
                  sources=["pytools/nms/_nms.pyx", "pytools/nms/nms.c"],
                  include_dirs=[numpy.get_include()])]

setup(
    ...
    packages=['pytools'],
    ext_modules=cythonize(exts)
)

Note that I wrap cythonized extension in cythonize() and use the full module name + full paths to extension sources. Also, since nms is a module in pytools package, including pytools.nms in packages has no effect.

查看更多
登录 后发表回答