Member Avatar for leegeorg07

i have python 2.5 and 3 and i want to use 2to3 to convert all of my programs to python3 but i dont know how to use it, if it helps in the Lib folder of python3 it is called lib2to3.

any help or examples welcome.

Recommended Answers

All 11 Replies

open a terminal and go to your code directory.

type in the following:

python path_to_2to3.py yoursourcefile.py

The tool will then output all the changes that it can perform.
If you want to write the changes back to the file, use this command instead

python path_to_2to3.py -w yoursourcefile.py

Take a look at http://docs.python.org/3.0/library/2to3.html for more information

Member Avatar for leegeorg07

i tried that in the cmd promt and it said that 'python wasnt a proper command' and im the python command line it said
invalid syntax and pointed to the

yoursourcefile.py

how can i fix this?

i tried that in the cmd promt and it said that 'python wasnt a proper command'

You need to use the full path to python.exe if it's not in your PATH variable...

So instead of python path_to_2to3 my_program.py you would need C:\\Python26\\python.exe path_to_2to3 my_program.py Or which ever version of Python is appropriate. Check your system in case you've installed python in a different directory.

Member Avatar for leegeorg07

thanks scru that got rid of the first error but now i have the error of attempted relative import of non package.

from . import item

how can i fix this?

thanks scru that got rid of the first error but now i have the error of attempted relative import of non package.

from . import item

how can i fix this?

What is that? Did you write it or is part of 2to3?

Member Avatar for leegeorg07

its part of 2to3, its in __main__, this is where it is in the module:

import sys
import os
import logging
import shutil
import optparse

from . import refactor

Are you using Python30\python.exe or Python25\python.exe to run 2to3? I think 3.0 changed the way imports work so this script probably depends heavily on those changes.

Member Avatar for leegeorg07

im using Python30

I simply wrote a small Python program to run with Python30 to convert some of my Python25 files ...

# run_2to3_convert.py
#
# convert a Python25 code file to a Python30 code file
# generates a backup file and overwrites the original
# file with the converted file
#
# it is best to put this file into a special directory
# together with the Python25 file you want to convert
#
# run this program with Python30

import subprocess

# the Python2x code file you want to convert ...
python2x_scriptfile = "TryExcept1.py"

subprocess.call([r"C:\Python30\Python.exe",
    r"C:\Python30\Tools\Scripts\2to3.py",
    "-w",
    python2x_scriptfile])

Also handles Tkinter GUI programs well.

commented: thanks great code +1
Member Avatar for leegeorg07

thanks that works great, i just edited it to convert all files in a folder:

# run_2to3_convert.py
#
# convert a Python25 code file to a Python30 code file
# generates a backup file and overwrites the original
# file with the converted file
#
# it is best to put this file into a special directory
# together with the Python25 file you want to convert
#
# run this program with Python30

import subprocess

# create a list of all the files in a given direcory
# and any of its subdirectories (Python25 & Python30)

import os
import os.path

def file_lister(currdir, mylist=[]):
    """
    returns a list of all files in a directory and any of
    its subdirectories
    note that default mylist becomes static
    """
    for file in os.listdir(currdir):
        # add directory to filename
        full_name = os.path.join(currdir, file)  
        if not os.path.isdir(full_name):
            mylist.append(full_name)
        else:
            # recurse into subdirs
            file_lister(full_name)
    return mylist

dir_name = r"C:\\" 
file_lists = file_lister(dir_name)
file_list = filter(lambda x: x.lower().endswith(".py"),file_lister(dir_name))
for file in file_list:
    # the Python2x code file you want to convert ...
    python2x_scriptfile = file

    subprocess.call([r"C:\Python30\Python.exe",
    r"C:\Python30\Tools\Scripts\2to3.py",
    "-w",
    python2x_scriptfile])

i hope this helps anyone else wondering about it

commented: very nice improvement +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.