I am new to Python, and decided to install Anaconda and Python v3. When I try to run examples I find on-line, they often don't work even when unchanged. I assume that the reason is sometimes that I try to run a version 2 script. What are some easy markers to look for in the code to tell if that is the case?
-
This is very broad; you basically have to learn the differences between 2 / 3. – Simeon Visser Jul 31 '15 at 14:33
-
1You could look for uses of `print` as a statement instead of a function. – Frédéric Hamidi Jul 31 '15 at 14:34
-
1The easiest marker for me is the `print` statement. If its a statement its Python 2 , if its a function its Python 3. – Anand S Kumar Jul 31 '15 at 14:34
-
4`print x` unambiguously identifies a script as Python 2, but `print(x)` doesn't unambiguously identify a script as Python 3, because it's valid syntax in both versions. – Kevin Jul 31 '15 at 14:37
-
1One thing that absolutely marks code as Python3 is function annotations. `def f(a: int, b: list) -> tuple: return (a,b)` will throw a syntax error in python 2 – NightShadeQueen Jul 31 '15 at 15:22
4 Answers
The easiest way (I find) to identify if a script is created for Python 3 vs Python 2 is to try to find a print statement, if the print statement has parentheses around the argument your script is made for Python 3 if not, it is made for Python 2.
E.g.
Python 2 code would look like
print "Hello World"
Where Python 3 code would look like print("Hello World")

- 9,474
- 36
- 90
- 105

- 185
- 12
-
1Yeah, ive been doing way too much C programming and its more of a habit. Ill just edit it out real fast. – Brennan Mcdonald Jul 31 '15 at 14:40
-
As pointed out in a comment above, this is evidence, but not proof. So what else is there to look for? – Student tea Jul 31 '15 at 14:43
-
Unless it's a `Python 2` code and the author used `from __future__ import print_function` – DeepSpace Jul 31 '15 at 14:48
-
4Although this is acceptable as a crude metric, `print("Hello world")` is also valid in Python 2, even without `from __future__ import print_function` and is becoming the preferred expression for those of us who are in some sort of transition from 2 to 3. – tripleee Jul 31 '15 at 14:51
-
And as a remark, for simple scripts, it is easy to write code correct for both Python2 and Python3. So what would they be called ? – Serge Ballesta Jul 31 '15 at 15:11
http://python3porting.com/differences.html
This lists the major differences you see betwen python 2 and 3. Mainly functions have been deprecated or renamed.
2to3 is a library which does the "translation" between both language versions. The documentation for the library lists the changes it does to translate python 2 to python 3 code.
The changed print, ne, dict and sys_exc are 4 differences I typically look at.

- 36,610
- 3
- 36
- 69
I'll give an SO-valid answer:
Apart from checking shebang (if any), the only sure-proof way is try to compile it with both versions as per How can I check the syntax of Python script without executing it?.

- 1
- 1

- 33,874
- 19
- 107
- 152