Python Forum
Migrating of python2 script to python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Migrating of python2 script to python3
#1
Sad 
Hello Fellow Python users i have a frontend code and other backend,libraries that i need to convert them from python2 to python3 but while trying using pip 2to3 i facking errors and i dont know how to use modernize or futurize functions, can someone help me with the conversion process. I'm attaching the screenshot of the error while using pip install.

Attached Files

Thumbnail(s)
   
Reply
#2
pip did drop support for Python 2.7 in version 21.0.
So have to install a older version pip install pip==20.0
There is no need to upgrade Python 2.7,if migrate older code you use 2to3 from a Python 3 version.
So need have Python 3 version where test(and use tool eg 2to3) that the older 2.7 code works.
Reply
#3
(Oct-05-2023, 11:45 AM)snippsat Wrote: pip did drop support for Python 2.7 in version 21.0.
So have to install a older version pip install pip==20.0
There is no need to upgrade Python 2.7,if migrate older code you use 2to3 from a Python 3 version.
So need have Python 3 version where test(and use tool eg 2to3) that the older 2.7 code works.

sorry I'm not so good at it, what exactly should i do now just run the code on python3 version and check whether it runs or not??
Reply
#4
(Oct-05-2023, 11:58 AM)zuri Wrote: what exactly should i do now just run the code on python3 version and check whether it runs or not??
Yes that's be the first step,you are on Windows install eg Python 3.11 it comes with 2to3 in ...\Python311\Tools\scripts\2to3.py
There is also a online version Python 2 to 3 converter

If i do a demo,i just use from the folder i posted where i save words.py.
C:\Python311\Tools\scripts
λ python 2to3.py -w words.py
C:\Python311\Tools\scripts\2to3.py:3: DeprecationWarning: lib2to3 package is deprecated and may not be able to parse Python 3.10+
  from lib2to3.main import main
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored words.py
--- words.py    (original)
+++ words.py    (refactored)
@@ -4,4 +4,4 @@
     if len(w) <= 3:
         filtered_words.append(w)

-print filtered_words
+print(filtered_words)
RefactoringTool: Files that were modified:
RefactoringTool: words.py
# words.py
words = ['eagle', 'albatross', 'mouse', 'dog', 'tiger']
filtered_words = []
for w in words:
    if len(w) <= 3:
        filtered_words.append(w)

print(filtered_words)
The code before that had clear change which is print.
# words.py
words = ['eagle', 'albatross', 'mouse', 'dog', 'tiger']
filtered_words = []
for w in words:
    if len(w) <= 3:
        filtered_words.append(w)

print filtered_words
Reply
#5
(Oct-05-2023, 12:32 PM)snippsat Wrote:
(Oct-05-2023, 11:58 AM)zuri Wrote: what exactly should i do now just run the code on python3 version and check whether it runs or not??
Yes that's be the first step,you are on Windows install eg Python 3.11 it comes with 2to3 in ...\Python311\Tools\scripts\2to3.py
There is also a online version Python 2 to 3 converter

If i do a demo,i just use from the folder i posted where i save words.py.
C:\Python311\Tools\scripts
λ python 2to3.py -w words.py
C:\Python311\Tools\scripts\2to3.py:3: DeprecationWarning: lib2to3 package is deprecated and may not be able to parse Python 3.10+
  from lib2to3.main import main
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored words.py
--- words.py    (original)
+++ words.py    (refactored)
@@ -4,4 +4,4 @@
     if len(w) <= 3:
         filtered_words.append(w)

-print filtered_words
+print(filtered_words)
RefactoringTool: Files that were modified:
RefactoringTool: words.py
# words.py
words = ['eagle', 'albatross', 'mouse', 'dog', 'tiger']
filtered_words = []
for w in words:
    if len(w) <= 3:
        filtered_words.append(w)

print(filtered_words)
The code before that had clear change which is print.
# words.py
words = ['eagle', 'albatross', 'mouse', 'dog', 'tiger']
filtered_words = []
for w in words:
    if len(w) <= 3:
        filtered_words.append(w)

print filtered_words
where should i run it? in cmnd prompt or on ide of pyth3?
Reply
#6
(Oct-05-2023, 12:41 PM)zuri Wrote: where should i run it? in cmnd prompt or on ide of pyth3?
From command line cmd,i use cmder but commands is the same.
Could also install 2to3,then if work from anywhere in cmd.
C:\
λ python --version
Python 3.11.3

C:\
λ pip -V
pip 23.2.1 from C:\python311\Lib\site-packages\pip (python 3.11)

C:\
λ pip install 2to3 --upgrade
Collecting 2to3
  Downloading 2to3-1.0-py3-none-any.whl (1.7 kB)
Installing collected packages: 2to3
Successfully installed 2to3-1.0

C:\
λ 2to3 --help
Usage: 2to3 [options] file|dir ...

Options:
  -h, --help            show this help message and exit
  -d, --doctests_only   Fix up doctests only
  -f FIX, --fix=FIX     Each FIX specifies a transformation; default: all
  -j PROCESSES, --processes=PROCESSES
                        Run 2to3 concurrently
  -x NOFIX, --nofix=NOFIX
                        Prevent a transformation from being run
  -l, --list-fixes      List available transformations
  -p, --print-function  Modify the grammar so that print() is a function
  -e, --exec-function   Modify the grammar so that exec() is a function
  -v, --verbose         More verbose logging
  --no-diffs            Don't show diffs of the refactoring
  -w, --write           Write back modified files
  -n, --nobackups       Don't write backups for modified files
  -o OUTPUT_DIR, --output-dir=OUTPUT_DIR
                        Put output files in this directory instead of overwriting the input files.  Requires -n.
  -W, --write-unchanged-files
                        Also write files even if no changes were required (useful with --output-dir); implies
                        -w.
  --add-suffix=ADD_SUFFIX
                        Append this string to all output filenames. Requires -n if non-empty.  ex: --add-
                        suffix='3' will generate .py3 files.
Reply
#7
(Oct-05-2023, 01:59 PM)snippsat Wrote:
(Oct-05-2023, 12:41 PM)zuri Wrote: where should i run it? in cmnd prompt or on ide of pyth3?
From command line cmd,i use cmder but commands is the same.
Could also install 2to3,then if work from anywhere in cmd.
C:\
λ python --version
Python 3.11.3

C:\
λ pip -V
pip 23.2.1 from C:\python311\Lib\site-packages\pip (python 3.11)

C:\
λ pip install 2to3 --upgrade
Collecting 2to3
  Downloading 2to3-1.0-py3-none-any.whl (1.7 kB)
Installing collected packages: 2to3
Successfully installed 2to3-1.0

C:\
λ 2to3 --help
Usage: 2to3 [options] file|dir ...

Options:
  -h, --help            show this help message and exit
  -d, --doctests_only   Fix up doctests only
  -f FIX, --fix=FIX     Each FIX specifies a transformation; default: all
  -j PROCESSES, --processes=PROCESSES
                        Run 2to3 concurrently
  -x NOFIX, --nofix=NOFIX
                        Prevent a transformation from being run
  -l, --list-fixes      List available transformations
  -p, --print-function  Modify the grammar so that print() is a function
  -e, --exec-function   Modify the grammar so that exec() is a function
  -v, --verbose         More verbose logging
  --no-diffs            Don't show diffs of the refactoring
  -w, --write           Write back modified files
  -n, --nobackups       Don't write backups for modified files
  -o OUTPUT_DIR, --output-dir=OUTPUT_DIR
                        Put output files in this directory instead of overwriting the input files.  Requires -n.
  -W, --write-unchanged-files
                        Also write files even if no changes were required (useful with --output-dir); implies
                        -w.
  --add-suffix=ADD_SUFFIX
                        Append this string to all output filenames. Requires -n if non-empty.  ex: --add-
                        suffix='3' will generate .py3 files.
when Im trying to get the version info this is what im getting even though i installed the python 3 version

Attached Files

Thumbnail(s)
   
Reply
#8
Look this tutorial .
Install this page Windows installer (64-bit) Windows Recommended
So you add to Path under install,if that not work you look under Fixing Path if needed.
How to change Environment Variables Path
So as this in this image just that you add path to where you have installed Python 3.11.
Then test in cmd again,also my need to restart Pc.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Kivy App - Python3 script to Android app (opencv) jttolleson 7 3,538 Oct-19-2023, 01:11 PM
Last Post: MasCain
  Migration of Python2 and Python3 using Modernize and Future Rakshan 5 2,539 Oct-05-2023, 08:55 AM
Last Post: zuri
  Migrating data from oracle into postgres python_student 1 2,454 Feb-10-2022, 09:16 PM
Last Post: buran
  python2 python3 messed up : How to fix ? hary 15 8,061 Dec-30-2020, 08:26 PM
Last Post: hary
  Getting a small Python2 prog to run in Python3 steve140 4 3,899 Apr-19-2020, 09:27 AM
Last Post: steve140
  output mismatching when porting a python from python2 env to python3 env prayuktibid 2 2,574 Jan-21-2020, 04:41 AM
Last Post: prayuktibid
  python3 decoding problem but python2 OK mesbah 0 1,808 Nov-30-2019, 04:42 PM
Last Post: mesbah
  Migrating to Mysql from SQlite atari400 10 4,622 Nov-23-2019, 09:56 PM
Last Post: atari400
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,927 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  Trying to run a python2 script dagamer1991 3 2,557 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