简体   繁体   English

python setuptools:如何用cython子模块安装包?

[英]python setuptools: how can I install package with cython submodules?

I have a python package named pytools . 我有一个名为pytools的python包。 It contains a cython-based submodule nms . 它包含一个基于cython的子模块nms

When I install the root package pytools with sudo python -H setup.py , the root package seems to be installed properly. 当我使用sudo python -H setup.py安装root软件包pytools时,root软件包似乎已正确安装。

But the installation didn't copy compiled nms.so to /usr/local/lib/python2.7/dist-packages/pytools/nms/ . 但是安装没有将已编译的nms.so复制到/usr/local/lib/python2.7/dist-packages/pytools/nms/

And When I import pytools in ipython, an error encountered: 当我在ipython中导入pytools时遇到错误:

ImportError: cannot import name nms ImportError:无法导入名称nms

If I manually copy the pytools/nms/nms.so to /usr/local/lib/python2.7/dist-packages/pytools/nms/ , the problem is solved. 如果我手动将pytools/nms/nms.so/usr/local/lib/python2.7/dist-packages/pytools/nms/ ,问题就解决了。

Here is my setup.py of the root package: 这是我的root包的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='zhaok1206@gmail.com',
  license='MIT',
  packages=['pytools', 'pytools.nms'],
  #packages=['pytools'],
  zip_safe=False
)

And setup.py of sub-package nms : 和子包nms setup.py

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. 这似乎是一个重复的问题, 试图为python包构建一个cython扩展,而不是创建共享对象(.so)文件 ,但我仍然想在这里发布它,因为那里没有太多的讨论。

Thank you! 谢谢!

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. 请注意,我在cythonize()包装了cythonized扩展,并使用完整的模块名称+扩展源的完整路径。 Also, since nms is a module in pytools package, including pytools.nms in packages has no effect. 此外,由于nmspytools包中的模块,包括pytools.nmspackages中没有任何效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM