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.
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.
# Python 2 and 3: forward-compatible from builtins import range for i in range(10**8): ... # Python 2 and 3: backward-compatible from past.
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.
Just open the python files in the pycharm editor, it will show warnings if the code is not compatible to Python2 or Python3.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With