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
you do this before you import anything else:
from __future__ import print_function
Now you just use the python3.x style print...