尝试将cython扩展构建为python包,而不创建共享库(.so)文件 [英] Attempting to build a cython extension to a python package, not creating shared object (.so) file

查看:139
本文介绍了尝试将cython扩展构建为python包,而不创建共享库(.so)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用答案

I have attempted to use the answer here to add the building of a cython extension into my package. It currently cythonizes the code to produce a .c file from the .pyx file but doesn't create a shared object .so file, as such when I try to import the package, and one of the modules attempts to import the shared object file it cannot find it.

我的setup.py文件(略有缩小)如下:

My setup.py file (this is slightly cut-down) is like so:

from setuptools import setup
from setuptools.extension import Extension
import os
import numpy
from Cython.Build import cythonize

mypackage_root_dir = os.path.dirname(__file__)
with open(os.path.join(mypackage_root_dir, 'requirements.txt')) as requirements_file:
    requirements = requirements_file.read().splitlines()

extensions = [Extension(
    name="package.submodule.foo",
    sources=["package/submodule/foo.pyx"],
    include_dirs=[numpy.get_include()],
    )
]

setup(name='package',
      version=0.1,
      description='...',
      author='my name',
      author_email='my email',
      url="...",

      include_package_data=True,
      packages=['package',
                'package.submodule1',
                'package.submodule2',
                'package.submodule', # the one that uses the pyx file
      ],
      ext_modules = cythonize(extensions),
      install_requires=requirements,
)

如何解决此问题,以便在运行python setup.py install时可以获取setup.py文件来构建共享库文件?

How can I fix this such that I can get the setup.py file to build the shared object file when python setup.py install is run?

推荐答案

我的问题中的代码正在工作,并且在安装位置的package/subpackage目录内构建.so文件.它找不到文件.但是,当我手动将文件移动到软件包的根安装目录的位置时,它起作用了.

The code in my question was working and building the .so file inside the package/subpackage directory in the install location, however, when I tried to import the package it couldn't find the file. However when I manually moved the file to the location of the root install directory of the package it worked.

因此,似乎要求共享库文件位于软件包的根目录中,而不是子模块目录中.

It appears to therefore require that the shared object file be in the root directory of the package rather than the submodule directory.

我可以通过更改扩展名定义来实现此目的,

I am able to achieve this by changing the extension definition like so:

extensions = [Extension(
        name="foo",
        sources=["package/submodule/foo.pyx"],
        include_dirs=[numpy.get_include()],
        )
    ]

这会将.so文件放在安装目录的根目录中.

This puts the .so file in the root install directory.

但是我不确定为什么它要求此共享库文件位于软件包的根目录中,而不是像常规python文件那样位于子软件包目录中.

However I'm not sure why it requires this shared object file to be in the root of the package rather than the subpackage directory as is the case with normal python files.

这篇关于尝试将cython扩展构建为python包,而不创建共享库(.so)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆