0

I have two blocks of code that I would like to combine into one python script. One block of code is written in Python 2.7, the other in Python 3.6

My question is, is there some way to call an old version of python mid script to run that specific block of code?

For instance, if I ran the following code, the script would error out because the second print statement is missing the parenthesis:

#Running python 2 and 3!!

#Py 2
print "Python 2"

#Py 3
print "Python 3"

Note: Converting the block of code written in 2.7 is possible, but will take a very long time; Time that I do not have at the moment.

Rodrigue
  • 3,617
  • 2
  • 37
  • 49
  • You might want to look at [Six](https://pythonhosted.org/six/) – bendl Feb 15 '18 at 15:51
  • 4
    There is no way to do this for two sections in the same file. You can put the second bit in another file and use `subprocess.call` – Phydeaux Feb 15 '18 at 15:51
  • 1
    you cannot put `print "Python 3"` in a python 3 script, even somewhere where it's not executed. – Jean-François Fabre Feb 15 '18 at 15:52
  • You can use [`py2to3`](https://docs.python.org/3/library/2to3.html) – MEE Feb 15 '18 at 15:53
  • so, naughty boy, you don't put shebang on top of your scripts? -)) – marmeladze Feb 15 '18 at 15:53
  • I didnt think you can switch versions mid scirpt with shebang, can you? – JimmyBuffet_Express Feb 15 '18 at 16:04
  • What makes you think running a single script in two different runtimes would be less work than simply converting everything to python 3? For small to medium projects it's usually not much that have to be rewritten. You could shell out parts of the script to a different process using `subprocess`, but you can't share memory between the processes, so for anything non-trivial, you would have to rewrite a lot of code anyway. – Håken Lid Feb 15 '18 at 16:04
  • Just always write `print ("Python Whatever")` – Olivier Melançon Feb 15 '18 at 17:23
  • @Olivier `print(1,2)` doesn't do the same thing in Python 2 and 3, unlike your example, but the OP could do `from __future__ import print_function` first, and then it would work consistently. – Mark Tolonen Feb 16 '18 at 08:46
  • @MarkTolonen, I know. I try using this so the syntax never fails. But I should have mentionned that as well, thanks you. – Olivier Melançon Feb 16 '18 at 13:09

2 Answers2

2

You could call your Python 2 snippet in a separate process from your Python 3 code (or vice versa) using the subprocess module. That would only work if you don't expect your Python context to be affected by the side effects of the Python 2 snippet, e.g. setting a variable from 2 that 3 will then use would not be possible.

If you wanted to use objects created by your Python 2 snippet in your Python 3 script, you could consider using pickle: serialize your objects to a local file in Python 2.7 and deserialize them from Python 3 - note that there are some changes between 2 and 3 that pickle will have to handle (see Unpickling a Python 2 object with Python 3).

Rodrigue
  • 3,617
  • 2
  • 37
  • 49
  • Is it possible to use pickle to access the object from the subprocessed script? How about writing to a flat file, then reading it in later on in the script? – JimmyBuffet_Express Feb 15 '18 at 17:33
  • That would work and there are some options in [pickle.load](https://docs.python.org/3/library/pickle.html#pickle.load) just for that purpose. I'll add that option to my response. – Rodrigue Feb 16 '18 at 08:38
1

To save you time: Automated Python 2 to 3 code translation

using 2to3 have worked wonders for me and is very simple to handle:

$ 2to3 example.py
Skandix
  • 1,916
  • 6
  • 27
  • 36