Calling a python file within pypy

送分小仙女□ 提交于 2021-02-05 10:42:05

问题


I recently moved to PyPy. It's amanzingly fast, but a lot of python libraries are not implemented yet. So I got a lot of home made python functions that I'd like to call within the PyPy code.

Here's my question: is there a way to call a python file or function within PyPy, and passing it some arguments ?

A code example:

I got a python module named python_code.py using a library not supported by PyPy, matplotlib for instance.

import matplotlib as mp

def my_custom_ploting_function(*args,**kwargs):
    some code

and I'd like to create a PyPY module named pypy_code.py like this:

from python_code import my_custom_ploting_function

def my_custom_pypy_ploting_function(*args,**kwargs):
    my_custom_ploting_function(*args,**kwargs)

But this code won't work, because PyPy cannot import the python_code module, because it thus will try to import matplotlib (which is not supported by PyPy).


回答1:


You can't expect to import modules and have them co-exist in the same program which would be running half CPython and half PyPy. However, what you can do is to run your program mainly in one of the two interpreters, and consider the other one as an additional library with which you communicate at a lower level than with Python objects.

For example, if you only want to use matplotlib to display some graphic, you can from PyPy start a CPython program (with os.system() or the subprocess module) and pass it the data to display in one way or another (e.g. by sending it into a pipe). If this is too limiting for what you want, there are other alternatives which are more involved. You can for example load libpython2.7.so inside PyPy and call its C API with CFFI. Or the reverse: embed PyPy inside CPython (as e.g. http://jitpy.readthedocs.org/en/latest/ ).




回答2:


You can't run (or import) python scritps that need modules not supported by pypy.

You actually can use matplotlib from within pypy, but it's very very hackish (and hard to do).

The simple answer here is just use plain python. If you're doing numeric manipulation, all intensive code should be inside numpy, anyway.



来源:https://stackoverflow.com/questions/27313588/calling-a-python-file-within-pypy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!