Home > Blockchain >  Backward compatibility with Modernize (Python2 to Python3) fails
Backward compatibility with Modernize (Python2 to Python3) fails

Time:02-22

I have a simple sample script written in python2. Since we are migrating to python3, I am trying to get familiar with the tools that ae present. The modernize tool, helps me to achieve a python3 code. I run it, I get the result expected.

However, modernize promises backward compatibility, I expect the newly generated code to run in python2 as well.

While running with python2 Interpreter, I face issues as shown below:

Eg: Sample.py



import Queue
from urllib2 import urlopen

def greet(name):
    print 'Hello',
    print name

print "What's your name?",
name = raw_input()
greet(name)

From the directory on the commandline

python-modernize -w Sample.py

New Sample.py:

from __future__ import absolute_import
from __future__ import print_function
import six.moves.queue
from six.moves.urllib.request import urlopen
from six.moves import input

def greet(name):
    print('Hello', end=' ')
    print(name)

print("What's your name?", end=' ')
name = input()
greet(name)

I run the new script from the command line. Produces correct results.

py Sample.py

However, since it is backward compatible, when I do the following I get errors:

C:\Python27\python.exe Sample.py

Traceback (most recent call last):
  File "Sample.py", line 3, in <module>
    import six.moves.queue
ImportError: No module named six.moves.queue

Should the new script be modified again? Is modernize not fully backward compatible? Please let me know

CodePudding user response:

Did you install the six module ?

pip install six

https://pypi.org/project/six/

CodePudding user response:

The answer for this is on the same regard as pip installing libraries in different versions. I was installing the library in python3. So now I managed to get it done on python2. Refer here

PythonDocumentation

  • Related