Python Forum

Full Version: Migration of Python2 and Python3 using Modernize and Future
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working on the migration of files from Python2.7 to Python3+. As a part of preparation, I have come across utility 2to3, Modernize and Futurize

2To3 seems to be very simple and I have managed to try out examples. Since it is not backward compatible, it is suggested that Modernize and Futurize are better

I installed the modernize library and Six as well, I have 2 versions of Python on my system (2.7 and 3.9)
I have a sample(Test.py) python2 code.

def greet(name):
    print "Hello, {0}!, How are you doing?" .format(name)
print "Your name?"
name = raw_input()
greet(name)
Now I go to the directory to run the command :
python-modernize -w Test.py
I observe that the python file is migrated to python3. When I run the code with the python3 interpreter from the command line, it works as expected.

New Test.py:
from __future__ import print_function
from six.moves import input
def greet(name):
    print("Hello, {0}!, How are you doing?" .format(name))
print("Your name?")
name = input()
greet(name)
However, since it promises backward compatibility, I expect it to run with Python2 interpreter as well
When I do the following :
C:\Python27\python.exe Test.py
I get an error:
Error:
Traceback (most recent call last): File "Test.py", line 2, in <module> from six.moves import input ImportError: No module named six.moves
I uninstalled and installed the six library, but no success. What am I doing wrong here?
Did you installed six in python2?
(Feb-21-2022, 12:20 PM)Axel_Erfurt Wrote: [ -> ]Did you installed six in python2?

I have downloaded the tar.gz file and extracted it to C:\Python27/Tools/Libs/site-packages
Rectified: problem was with the mulitple versions. I had to install the library in python2. but my pip install command was installing it in python3. After referring the document: https://docs.python.org/3/installing/ind...hlight=pip, I could solve my problem
Rakshan Wrote:Since it is not backward compatible, it is suggested that Modernize and Futurize are better
Unless you have a very good reason to do so, trying to be backward compatible is a strange idea. If you want to migrate to Python 3, dive into it ! Trying to write code that works both for Python 2 and 3 looks harmful to this aim. It will produce heavier and lower quality Python 3 code.

Just my two cents...
hi Rakshan i tried to upgrade my code using the pip 2to3 function but it does not seems to be working can you guide me on how to use modernize and futurize functions.