Python Version History
Let’s have a detailed understanding about Python version 2 release dates and Python 3 release dates
|S. No||Python 2 Version & Release Date||Python 3 Version & Release Date|
|1||Python 2.0 - released on October 16, 2000||Python 3.0 - released on December 3, 2008|
|2||Python 2.1 - released on April 17, 2001||Python 3.1 - released on June 27, 2009|
|3||Python 2.2 - released on December 21, 2001||Python 3.2 - released on February 20, 2011|
|4||Python 2.3 - released on July 29, 2003||Python 3.3 - released on September 29, 2012|
|5||Python 2.4 - released on November 30, 2004||Python 3.4 - released on March 16, 2014|
|6||Python 2.5 - released on September 19, 2006||Python 3.5 - released on September 13, 2015|
|7||Python 2.6 - released on October 1, 2008||Python 3.6 - released on October 2016|
|8||Python 2.7 - released on July 3, 2010||Python 3.7 - released on June 2018|
Python 2 vs Python 3
|S. No||Key Comparison||Python 2||Python 3|
|2||Syntax||The syntax of Python 2 was difficult to understand||The syntax of Python 3 is simpler and easier|
|3||Print Function||print "hello"||print ("hello")|
|4||Integer Division||When two integers are divided, you always get an integer value.||Whenever two integers are divided, you always get a float value|
|5||Unicode||To store Unicode string value, you would require to define them with "u".||In Python 3, default storing of strings is Unicode.|
|6||Rules of ordering Comparisons||Rules of ordering comparison are very complex.||In this version, Rules of ordering comparisons is very easier|
|7||Xrange||In Python 2, the xrange () is used for iterations.||The new Range () function introduced to perform iterations.|
|8||Returning iterable objects instead of lists||In Python 2, some function & methods return lists||In Python 3, some function & methods return iterable objects|
|9||Parsing user inputs via input ()||raw_input () to be used in order to read other data types than String||It always stores the user input as Str objects|
|10||Rounding||In Python 2, decimals are rounded to the nearest number (No matter be it a Odd or Even number)||In Python 3, decimals are rounded to the nearest even number.|
|11||Raising Exceptions||Python 2 accepts both notation (Old & New Syntax). Meaning it should be enclosed in notations or parenthesis.||It should be enclosed in parenthesis.|
|12||Handling Exceptions||No need to use any keyword Eg: print 'Python', python_version () try: NameError except NameError, err: print err, '--> our Error message'||Need to use "as" keyword to handle the exception Eg: print ('Python', python_version ()) try: NameError except NameErroras err: print (err, '--> Error message')|
|13||Leak of variables||The value of the global variable will change while using it inside for-loop.||The value of variables never changes.|
|14||Backward compatibility||Python version 3 is not backwardly compatible with Python 2.||Not difficult to port python 2 to python 3 but it is never reliable.|
|15||Library||Many older libraries created for Python 2 is not forward-compatible.||Many recent developers are creating libraries which you can only use with Python 3.|
Why to Choose Python 3In Python, Version 3 is the future, here are some of the reason why to choose Python 3
- Legacy Python 2 code will no longer be maintained after 2020
- Upgrade option is available in Python 3
- Easier to write a code
- Avoid the syntax confusion
- Improved number management helps to perform math calculation
- Lot of popular add-ons are supported
- Unicode’s are supported
Porting Process / Migrating Process – From Python 2 to Python 3Porting / Migrating the code from Python 2 to Python 3 involves only 3 steps. They are
- Auto Conversion
- Manual Changes
- Runtime Validation & Fix
- Python 3 team has provided a new tool called 2to3tool where it reads the Python 2.x source and apply some fixers to convert it in to Python 3.x code where the common syntax change will be done by the auto conversion tool.
- Library named “lib2to3” contains rich set of fixers which will handle almost all the code.
- For example, you have a python 2 source file named example.py and if you want to convert it in to python 3 then we can use the library 2to3 in the command line as below $2to3 example.py This will do necessary modifications in the Source file
- If you ask Auto Conversion will do complete porting then the answer is “No, it will not do complete porting”.
- We need to run the Test Class and need to see if there are any errors. If error exists then that needs to be fixed accordingly based on the switching or comparison issue where the key name to be fixed in the function.
- To be honest, this is an iterative process or approach until all the code runtime has been validated.
- Once the common pattern has been identified, then there is another process named as “grep” & “sed” to replace across many files.
Runtime Validation & Fix:
- Once all the conversion process has been done, then we need to perform lot of code runtime validation. This will help us to identify the things earlier which might cause an issue.
- As python is a dynamic language, it is better to avoid visual static code inspection / changes.
Common Runtime Problems and it’s solution:Problem 1:
While porting, we may face “Type errors” during Runtime like bytes instead of str and vice versa
We need to see the variable type in the statement and based on that use, xxx. encode () to get bytes or xxx. decode () to get strings If input is not known, then use try xxx. encode () except Attribute Error: pass
Type Error: root – ERROR – ——->installation failed: a bytes-like object is required, not ‘str’
Change the byte type to string type or add b as prefix to the string under comparison
AttributeErrorsuite_setUp () or suite_tearDown () is missing for some test suites.
Add the dummy suite_setUp () and suite_tearDown () methods.
Tab Error: inconsistent use of tabs and spaces in indentation
Search for tab characters and replace with space characters.
Type Error: ‘FileNotFoundError’ object is not sub-scriptable
In Python 3, FileNotFoundError is not sub-scriptable so use errno attribute, e.errno
Steps to be performed post migration:
- Caniusepython3 will help to check which software dependencies will block you from supporting Python 3 using the tools provided.
- Futurize or Modernize will make sure that your Python 2 code is compatible with Python 3
- In setup.py file, one can update the classifiers which contain Programming Language:: Python :: 3 to indicate your code supports Python 2 and 3.
- Automating tests using tox will help you to understand that code stays compatible with Python 2 and 3.