hulyce
hulyce

Reputation: 470

Calling a python file within pypy

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).

Upvotes: 0

Views: 953

Answers (2)

Armin Rigo
Armin Rigo

Reputation: 12945

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/ ).

Upvotes: 1

loopbackbee
loopbackbee

Reputation: 23322

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.

Upvotes: 1

Related Questions