How to embed python2 into python3?

Chris Rebert clp2 at rebertia.com
Wed May 16 14:00:22 EDT 2012


On Wed, May 16, 2012 at 7:59 AM, ytj <ytj000 at gmail.com> wrote:
> Hello, all:
>
> I have two programs, one is written in py3k, the other is written in
> python 2. I am wondering how to make them work together except port
> the python 2 code to py3k?

Porting the Python 3 code to Python 2 is also an option:
http://pypi.python.org/pypi/3to2

But unless you're dependent on a library not available for Python 3
yet, the normal 2to3 route is probably preferable.

In either direction, this library may help ease the transition:
http://pypi.python.org/pypi/six/

> Is that possible to expose python2's
> function to py3k? In other words, I want to embed the Python 2
> interpreter into my py3k program. So I can call python2's function in
> py3k's code.

That's very likely possible using the C API; I don't know of any
prepackaged solution though.
Another obvious approach to connect the two interpreters would be to
use some form of IPC or RPC (e.g. XML-RPC, Unix domain sockets, etc.).
There will be some degree of extra overhead involved no matter which
route you go.

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list