1

I was wondering if something like this possible:

import sys    
if sys.version[0] == '2':
    print 'this would fail in python3'
if sys.version[0] == '3':
    print("and this would fail in 2")

Now, if this is executed either python2 or python3 would fail when running this code. Is there a way to run a piece of code only if it's the right version, completely ignoring it if it is the wrong version?

Mitchell van Zuylen
  • 3,905
  • 4
  • 27
  • 64
  • If you try this with Python3, you're gonna have an error due to missing parentheses (line 3) for sure – Nuageux May 31 '17 at 09:31
  • Yes, that's the point. Is there a way to not have python3 check the piece of code intended for python2, and vice versa? – Mitchell van Zuylen May 31 '17 at 09:33
  • 1
    Change L3 to `print('this would fail in python3')` and it will run (for both Python 2 and 3) – Nuageux May 31 '17 at 09:34
  • 1
    I'm afraid that if you're using syntax only supported by python2 (e.g. the print without brackets) or python3 (e.g. starred expressions) the program would always give syntax errors and fail to run. You could try sticking to syntax supported by both versions as pointed out by others. – Work of Artiz May 31 '17 at 09:41
  • Maybe [this link](http://sweetme.at/2013/10/21/how-to-detect-python-2-vs-3-in-your-python-script/) can help you for what you want to do – codingfish May 31 '17 at 09:44

4 Answers4

1

If I understood you correctly the answer is no, you cannot run python code by python3 and ignore the bits intended to be run by python2 only like the print statement without parentheses.

However, you can split the code into 2 separate modules and make checks before running either of them using python3 or python2.

A better option is write code what is compatible both with python3 and python2.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
1

It's impossible to make Python 3 ignore line

print 'this would fail in python3'

as there's syntax error from Python3's point of view. The program is parsed first as a whole (even lines that are not executed at runtime), then executed. Syntax error makes it impossible to parse.

You have to make your code syntactically correct for both Python 2 & 3. For example, you can do

from __future__ import print_function

Then

print(…)

will work in both Python 2 and 3 identically.

Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78
  • 1
    You don't need the print function to make the syntax correct in Python 2 and 3, just need parentheses around both print calls – Chris_Rands May 31 '17 at 09:42
  • @Chris_Rands, yes, that's correct. But the behaviour will be different in 2 and 3 and this can cause errors, so I'd prefer import `print_function`. – Ilya V. Schurov May 31 '17 at 09:45
0

You can import the print function for python 3 in python 2 as well.

from __future__ import print_function
import sys    
if sys.version[0] == '2':
    print('this would fail in python3')
if sys.version[0] == '3':
    print("and this would fail in 2")

It will work perfectly in both python versions.

0

I use the following snippet for places where I really have to have multiple code to support both versions 2 and 3.

from __future__ import print_function
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

import sys

if (sys.version_info > (3, 0)):
    # Python 3 code here
    print("Code for Python3 won't work in py2")
else:
    # Python 2 code here
    print("Code for python3 won't work here")

I

jnvilo
  • 157
  • 1
  • 10