1

I'd like to make a Python program which runs on both 2.7 and 3.4 versions. I have only few changes to make that work like the print("", end="") for instance.

Is there a tag like

if __name__ == "__main__":

or something, that I can put into my code to get something like :

1. python_version = platform.version_tuple()
2. if python_version[0] == 3:
3.     print("myprint", end="")
4. else:
5.     print "my second print",

How can I avoid any compilation error with this code ?


Thank you for all your answers, my question wasn't very clear but you did help. I've found this detailled website after reading your answers :

http://python3porting.com/noconv.html

It explains how to handle both python2 and python3 into the same code without dividing it in 2 redundant parts.

The right way to do it is to use the sys.version

1. import sys
2. if sys.version < '3':
3.     #some code for python 3.X
4. else:
5.     #some code for older python

Or use the print_function from the future library

1. from __future__ import print_function

for prints issues and the ImportError error as follow

1. try:
2.     from urllib.request import urlopen
3. except ImportError:
4.     from urllib import urlopen

Thanks again for your help

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Lucas
  • 65
  • 1
  • 6
  • See also: http://stackoverflow.com/questions/12162629/using-print-in-python2-x – mgilson Jun 13 '14 at 09:43
  • You can use parens around the print statement in python2 as well. But if you want to use syntax in one version that does not compile with the other version, you must put it in a separate module and only import it if the version matches. – l4mpi Jun 13 '14 at 10:18
  • See also: [Python 2 and Python 3 dual development](http://stackoverflow.com/questions/11372190/python-2-and-python-3-dual-development) – NoDataDumpNoContribution Jun 13 '14 at 11:12

2 Answers2

3

you do this before you import anything else:

from __future__ import print_function

Now you just use the python3.x style print...

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Thanks for the answer ! However it doesn't solve entierely my problem, I have for instance a urllib.request import which isn't supported in Python 2.X. Isn't there any way to tell the compilator "compile this part of code only if you're using python X.X" ? – Lucas Jun 13 '14 at 09:53
  • @Lucas -- For that, you can rely on catching `ImportError`, or by looking at `sys.version` as you've demonstrated in the question and only importing the stuff that works on your version. The reason you need `__future__` is for those corner cases where the exception gets thrown _before_ you have a chance to handle it (e.g. python2.x's `print` throws a SyntaxError in the _parser_ which you can't handle) – mgilson Jun 13 '14 at 10:11
0

U can use sys

write two codes say python3.py and python2.py

import sys   
a= sys.version_info
if a.major=='2':
   import python2
   #run functions of python 2 this contain code compiles in python2
else:
   import python3
   #run functions of python 3 this contain code compiles in python3

to make more easy give every method name same in both the python code .and call them here

sundar nataraj
  • 8,524
  • 2
  • 34
  • 46
  • Thanks for the answer, however it does the same thing as the tuple i'm already using. My problem is that with the code above, I can't compilate neither in 2.X nor 3.X due to the use of both language. What I would like is a way to tell the compilator "read this if python version is 2.X and read that if it is 3.X" so that I won't have any compilation error. – Lucas Jun 13 '14 at 10:05