本文介绍了python setuptools:如何安装带有cython子模块的软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为的python包。
它包含一个基于cython的子模块。

I have a python package named pytools.It contains a cython-based submodule nms.

当我使用 sudo python -H setup.py 安装根软件包pytools时,
似乎已正确安装了根软件包。

When I install the root package pytools with sudo python -H setup.py,the root package seems to be installed properly.

但是安装未将编译后的 nms.so 复制到 / usr / local /lib/python2.7/dist-packages/pytools/nms /

But the installation didn't copy compiled nms.so to /usr/local/lib/python2.7/dist-packages/pytools/nms/.

当我在ipython中导入pytools时,遇到错误:

And When I import pytools in ipython, an error encountered:

如果我手动将 pytools / nms / nms.so 复制到 /usr/local/lib/python2.7/dist-packages/pytools/ nms / ,问题解决了。

If I manually copy the pytools/nms/nms.so to /usr/local/lib/python2.7/dist-packages/pytools/nms/, the problem is solved.

这是我的 setup.py 根包:

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='[email protected]',
  license='MIT',
  packages=['pytools', 'pytools.nms'],
  #packages=['pytools'],
  zip_safe=False
)

setup.py 子软件包 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()])],
)

这似乎是重复的问题,其中,而不是创建共享对象(.so)文件,但是我仍然要在此处发布它,因为那里没有太多讨论。

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.

谢谢!

推荐答案

您不需要子包中的安装脚本。只需在根设置脚本中构建扩展名即可:

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

请注意,我将cythonized扩展包装在 cythonize()中,并使用完整的模块名+扩展源的完整路径。另外,由于 nms pytools 包中的一个模块,其中包括 pytools.nms 包中的c $ c> 无效。

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.

这篇关于python setuptools:如何安装带有cython子模块的软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 11:51