简体   繁体   English

如何在PyPy中导入.so模块?

[英]How can I import a .so module in PyPy?

I am green to PyPy, and I use PyPy1.9 to interpret my Python code. 我对PyPy很满意,并且使用PyPy1.9解释我的Python代码。

As you know, the CPython interpreter can import a module packaged in a .so file, which is compiled from C/C++ codes. 如您所知,CPython解释器可以导入打包为.so文件的模块,该文件是从C / C ++代码编译而成的。

But PyPy1.9 interpreter tell me he can not find the module in a .so file, my code is a little slow and I want to optimize the performance, help!! 但是PyPy1.9解释器告诉我他在.so文件中找不到该模块,我的代码有点慢,我想优化性能,帮助!

First, CPyExt has been in PyPy since… I forget the exact version, but a couple years now, well before 1.9. 首先, CPyExt从那时起就一直在PyPy中使用……我忘记了确切的版本,但是几年后,即1.9之前。

Basically, all you have to do is build and install the module, just as you do with CPython. 基本上,您需要做的就是构建和安装模块,就像使用CPython一样。

Make sure you do the install with PyPy, not with CPython. 确保使用PyPy而不是CPython进行安装。 Besides the fact that each Python installation on your system has a separate site-packages directory, CPyExt requires modules to be built against the PyPy headers and libs, not the CPython ones. 除了系统上的每个Python安装都有一个单独的site-packages目录外,CPyExt还要求针对PyPy标头和库而不是CPython的库构建模块。

If you're installing manually, just do pypy setup.py install instead of python setup.py install . 如果您是手动安装,则只需执行pypy setup.py install而不是python setup.py install If you're using pip or easy_install , the PyPy versions of those tools are usually named something like pip_pypy and easy_install_pypy . 如果您使用的是pipeasy_install ,则这些工具的PyPy版本通常被命名为pip_pypyeasy_install_pypy

So, if you've installed pip , it should be as easy as: 因此,如果您已经安装了pip ,它应该很简单:

pip_pypy install foo

(Possibly with a sudo , of course.) (当然,可能带有sudo 。)

If you haven't installed pip , install it for PyPy the same way you would for CPython. 如果尚未安装pip ,则将其安装到PyPy的方法与安装CPython的方法相同。 For example: 例如:

curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
pypy get-pip.py

If you're using a binary installer, then obviously you have to get the binary installer for PyPy-1.9. 如果您使用的是二进制安装程序,那么显然您必须获得PyPy-1.9的二进制安装程序。 And, if there isn't one, you can't use a binary installer. 而且,如果没有,则不能使用二进制安装程序。 (Just like if you wanted to install foo for CPython 2.7 on 32-bit Windows and they only had a 2.6 package or a 64-bit package.) (就像您要在32位Windows上为CPython 2.7安装foo一样,它们只有2.6软件包或64位软件包。)


One last note: There are some packages that do not yet work with CPyExt. 最后一点:有些软件包尚未与CPyExt一起使用。 Most notably, numpy and everything that depends on it, and anything else that requires binding Fortran code. 最值得注意的是, numpy及其相关的所有内容,以及需要绑定Fortran代码的其他任何内容。 Some of these packages will check for CPython (or check for non-PyPy?) and give you an early error. 其中一些软件包将检查CPython(或检查非PyPy?),并为您提供早期错误。 Others may try to build and fail. 其他人可能会尝试建立并失败。 Others may successfully build but not work in some cases (eg, some of them fail in any program with more than 1 thread, because they try to get fancy with the GIL). 其他人可能会成功构建但在某些情况下不起作用(例如,其中某些人在任何具有1个以上线程的程序中均失败,因为他们试图获得GIL的青睐)。 But the easiest thing to do is try it and see. 但是最简单的方法是尝试一下然后看看。

For PyPy you would need to utilize something like CFFI , which uses Foreign Function Interface to access library code (see the section on Loading Libraries for more information). 对于PyPy,您需要利用CFFI之类的东西 ,它使用Foreign Function Interface来访问库代码(有关更多信息,请参见“ 加载库 ”部分)。

Edit : I stand corrected by the above answer. 编辑 :我站在上面的答案纠正。 I'll leave this answer here though to have an alternative for accessing libraries and simply accept justified downvotes. 我将在这里留下这个答案,尽管它有访问库的替代方法,并且只接受合理的降票。

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

相关问题 如何从 pyc 文件或 Pypy 中的 so 文件导入模块? - How can I import a module from a pyc file or so file in Pypy? 如何将 .py 文件中的变量导入列表,以便可以使用该列表中的随机模块? - How can I import variables from a .py file into a list, so that I can use the random module on that list? 如何在pypy中注释课程? - How can I annotate a class in pypy? 如何为pypy构建pyquery? - How can I build pyquery for pypy? 如何在不使用 ctype Linux 的情况下将 so 文件作为模块导入? - How can I import so file as a module without using ctype Linux? 如何构建Python C扩展,以便我可以从模块中导入它 - How to build a Python C Extension so I can import it from a module 如何在pypy沙箱中导入numpy - How to import numpy in pypy sandbox 如何使用pypy导入bitarray? - How to import bitarray using pypy? pypy导入任何模块时出现“utf8'编解码器无法解码字节0xcd位置” - There is "utf8' codec can't decode byte 0xcd in position" when pypy import any module 我在哪里放置来自Boost.Python的.so文件,以便将其作为模块导入,如何将它与Python 2和3一起使用 - Where do I put the .so file from Boost.Python so I can import it as a module, and how do I use it with both Python 2 and 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM