您可以导入.so文件吗? [英] Can you cimport an .so file?
问题描述
我有一个要从外部库导入的名为 tissue-classifier.cpython-37m-x86_64-linux-gnu.so
的.so文件,因此我可以在我的本地课程之一中扩展它。由于我正在扩展一个类,因此需要使用 cimport
将其作为扩展类型导入,我想知道是否有可能。如果我使用普通的 import
语句,那么我将得到一个Python编译版本,该版本不能用于扩展 cdef
当前文件中的类。
I have an .so file called tissue-classifier.cpython-37m-x86_64-linux-gnu.so
from an external library that I want to import so that I can extend it in one of my local classes. Since I am extending a class, I need to import it as an extension type using cimport
and I am wondering if this is even possible. If I use a normal import
statement then I will be left with a Python compiled version which cannot be used to extend a cdef
class in my current file.
当我尝试 cimport
tissue_classifier
文件,它给了我一个错误,即找不到 tissue_classifier.pxd
文件,这是有道理的,因为它位于 .so
格式。抱歉,如果这是一个愚蠢的问题,我只是有一段时间无法解决这个问题。
When I try to cimport
the tissue_classifier
file, it gives me the error that tissue_classifier.pxd
file could not be found which makes sense since it is in .so
format. Sorry if this is a dumb question, I just haven't been able to figure this out for a while.
推荐答案
不, * .so文件无法导入。
No, a *.so-file cannot be cimported.
如果使用C / CPP背景,则pyx / pxd / so-business最容易使用以下模型:
If one is having C/CPP-backgrounds, then pyx/pxd/so-business is propably easiest to understand using the following model:
- 生成的扩展名(
*。so
-文件)对应于C / CPP世界中的最终工件,它可能是可执行文件,共享库(*。so
)或库/目标文件集合。如果您只运行生成的程序,则只需要它。例如,您可以使用(并且可能使用)CPython解释器,而无需构建它或获取其源代码。以此类推,如果您具有二进制扩展名(*。so
),则可以导入并使用它,而不必构建它(甚至在上面具有相应的pyx文件或编译器)您的机器)-这就是轮子所提供的。。 li>
-
*。pyx
对应于c / cpp文件,该文件具有功能定义。如果要从源代码生成结果工件,则需要这些文件。在C / CPP世界中,此构建过程将通过使用make
或类似方法来触发。如果通过python setup.py install
安装软件包,则需要pyx文件-相当于调用make
。 -
*。pxd
对应于标头(h / hpp文件):它描述了生成的so-file中的功能,因此可以重复使用。例如,仅拥有CPython解释器不足以构建扩展-必须安装dev版本,因此还包括Python.h
& Co。
- the resulting extension (
*.so
-file) corresponds to the final artefact in C/CPP-world which could be an executable, a shared-object (*.so
), or a library/object-file collection. If you just runs the resulting program it is all you need. For example you can use (and probably do) a CPython-interpreter without having built it or having its source code. In analogy, if you have a binary extension (*.so
) you can import and use it whitout having to build it (or even having corresponding pyx-files or a compiler on your machine) - that is what is provided by a wheel. *.pyx
corresponds to c/cpp-files, which have the definitions of the functionality. These files are needed if you want to build the resulting artifact from the source. In C/CPP-world this build process would be triggered by usingmake
or similar. pyx-files are needed if you install the package viapython setup.py install
- which corresponds to callingmake
.*.pxd
corresponds to headers (h/hpp-files): it decribes the functionality in the resulting so-files, so it can be reused. For example just having CPython-interpreter isn't enough to build extensions - one has to install the dev-version so also the includesPython.h
&Co. are present at the machine.
那么可以做什么?
第一种可能性:
如果软件包作者考虑 *。 pxd
-文件是公共API的一部分,则可以将相应的pxd文件放在*旁边这样的文件进入安装程序,因此可以使用/扩展模块的c接口。
If authors of the package consider *.pxd
-files being part of the public API, then they can put the corresponding pxd-files next to *.so-files into the installation, so the c-interface of the module can be used/extended.
如果不放置pxd文件进入安装过程,因此很有可能该c接口只是实现细节,您不应该使用它,因为将来它可能会更改,恕不另行通知。
If they don't put the pxd-file into the installation, so chances are high that this c-interface is an implementation detail and you should not be using it, as it may change without notice in the future.
但是,可能会冒着风险,并手动将必要的pxd文件复制到安装中,但首先要确保它是正确的pxd版本(即与安装中的so-file生成版本相同) 。
However, it is possible to take the risk and to copy the necessary pxd-files to the installation per hand, but making first sure that it is the right pxd-version (i.e. the same with which so-files in the installation were built).
第二种可能性:
确保使用正确的pxd-version的最简单方法是从源代码构建软件包,即
The easiest way to ensure, that the right pxd-version is used is to build package from the source, i.e.
- 下载正确的来自github的版本(主版本或最新版本)
- 调用
python setup.py install
或自述文件告诉您的操作
- dowloading the the right versioin from github (master or last release)
- calling
python setup.py install
or what README tells you to do
现在,与其将pdx文件复制到安装中,还可以通过 include_path
进行cythonize功能,或将其添加到 sys.path
。
Now, instead of copying pdx-files to the installation, one could add include_path to the downloaded package-source via include_path
for cythonize-function or by adding it to sys.path
.
@BeforeFlight在评论中指出,可以使用 python setup.py开发
(或 pip install -e
Alternatively, as @BeforeFlight has pointed out in the comments, one could use python setup.py develop
(or pip install -e
the same folder so it can be deinstalled), and because it creates a link instead of copying data, the pxd-files will be found.
以上解决方案将有助于构建模块,但是分发模块则完全不同。
The above solutions will help to build the module, distributing it is a completely different story however.
这篇关于您可以导入.so文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!