0

A python implementation able to run both 3.x and 2.x code would be quite convenient.

PyPy is planning this for the future, but currently it only supports v2.x.

As alternative, is there any wrapper launcher that is able to perform on-the-fly code conversion (like 2to3 and 3to2)?

E.g. for running python3 code on python2 i've written this:

python -c "from __future__ import absolute_import, division, print_function, unicode_literals; execfile(\"$1\");"
1

For code that you need to run on python 2 and 3 there is the six library with documentation here.

It handles things like all of your imports but also copes with things like name changes between a lot of the standard library items for you.

Also PEP 397 deals with creating a python launcher for windows that is shebang aware so python scripts that start with #! /usr/bin/python2 would be launched under python 2, etc.

The Python 3.3 installer and later includes an implementation of this that will allow py filename to launch the required python for processing the script, (just like Linux, etc.).

You can also use the py launcher with command line overrides such as:

  • py -2 somefile.py3 launch a python 3 file under python 2
  • py -3 somefile.py force latest python 3 to run a file even if the shebang says to use python 2
  • py -2.6-32 somefile.py force the use of python 2.6 32 bit assuming it is installed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.