Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 2to3 properly for python?

I have some code in python 2.7 and I want to convert it all into python 3.3 code. I know 2to3 can be used but I am not sure exactly how to use it.

like image 945
GhostFrag1 Avatar asked Dec 08 '13 19:12

GhostFrag1


People also ask

What is 2to3 in Python?

2to3 is a Python program that reads Python 2. x source code and applies a series of fixers to transform it into valid Python 3. x code. The standard library contains a rich set of fixers that will handle almost all code.

Are Python 2 and 3 compatible with each other?

# Python 2 and 3: forward-compatible from builtins import range for i in range(10**8): ... # Python 2 and 3: backward-compatible from past.

How do I know if I have Python 2 or 3?

If you want to determine whether Python2 or Python3 is running, you can check the major version with this sys. version_info. major . 2 means Python2, and 3 means Python3.

How do I know if Python 3 is compatible?

Just open the python files in the pycharm editor, it will show warnings if the code is not compatible to Python2 or Python3.


1 Answers

Install the following module which adds the 2to3 command directly to entry_points.

pip install 2to3 

As it is written on 2to3 docs, to translate an entire project from one directory tree to another, use:

2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode 
like image 103
Faruk Sahin Avatar answered Oct 02 '22 00:10

Faruk Sahin