Python Forum
Migration of Python2 and Python3 using Modernize and Future
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Migration of Python2 and Python3 using Modernize and Future
#1
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?
Reply
#2
Did you installed six in python2?
Reply
#3
(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
Reply
#4
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
Reply
#5
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...
Reply
#6
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.

Attached Files

Thumbnail(s)
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 268 Mar-07-2024, 03:40 PM
Last Post: deanhystad
Sad Migrating of python2 script to python3 zuri 7 869 Oct-05-2023, 02:40 PM
Last Post: snippsat
  PyPi Download Counts and Warehouse Migration? tlee0818 1 1,250 Mar-20-2022, 07:41 PM
Last Post: snippsat
  db migration(db version control) for python lubaz 2 2,699 May-30-2021, 01:36 PM
Last Post: lubaz
  python2 python3 messed up : How to fix ? hary 15 7,837 Dec-30-2020, 08:26 PM
Last Post: hary
  Getting a small Python2 prog to run in Python3 steve140 4 3,796 Apr-19-2020, 09:27 AM
Last Post: steve140
  output mismatching when porting a python from python2 env to python3 env prayuktibid 2 2,515 Jan-21-2020, 04:41 AM
Last Post: prayuktibid
  python3 decoding problem but python2 OK mesbah 0 1,767 Nov-30-2019, 04:42 PM
Last Post: mesbah
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,820 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  Trying to run a python2 script dagamer1991 3 2,484 Aug-12-2019, 12:33 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020