How to use/install python 2to3?
Asked Answered
E

6

29

From this https://docs.python.org/3.4/library/2to3.html it says that 2to3 should be installed as a script alongside the python interpreter. However, in my /usr/bin/ folder there are no 2to3 executable, and running find from / finds no 2to3 executable either. I do however have a folder called lib2to3 at /usr/lib64/python{3.4/2.7}/lib2to3 but containing nothing I found relevant. In python/site-packages/setuptools/ there is a lib2to3_ex.py script, but nothing happens if I run it. How do I get to the point where I can simply type 2to3 upgradethisscripttopython3.py ?

Ectophyte answered 5/7, 2015 at 9:28 Comment(3)
Have you looked in Tools/scripts, per the docs? What happens if you just type 2to3 in the command line and hit return?Bulky
Tools/scripts often is not in your PATH, you might want to add it.Wise
I don't have the Tools/scripts path :S , and as far I have found out, the Tools/scripts files are downloaded with the python2.7-examples package in Ubuntu, but so far I haven't found the equivalent package for opensuse (which I'm using)Ectophyte
P
17

You need to first install the following packages:

apt install 2to3
apt install python3-lib2to3
apt install python3-toolz

For windows just install 2to3

pip install 2to3

Then, You can simply go to that directory your python file is in and type the following command:

2to3 ./filename.py

OR for writing the updated python 3 code to the existing file

2to3 . -w

This last will convert all the python files that are in the directory.

Paratroops answered 23/10, 2018 at 14:12 Comment(3)
toolz is unrelated to lib2to3Agneta
In Windows, I seemed to only need 2to3, not the other 2 packages.Plumbery
The pip variant can be used not only on Windows.Garret
E
7

So the solution is that at least not for me, the Tools/scripts files were not automatically installed. On ubuntu this would be solved by installing python-examples which on opensuse is called python-demo

Ectophyte answered 5/7, 2015 at 21:9 Comment(2)
on Fedora/RedHat yum install python-toolsGoodale
And on newer versions of Ubuntu, apt-get install 2to3.Teodora
R
5

On CentOS (and other RHEL-like distros) you will need to run yum install python-tools, which installs 2to3 to /usr/bin/2to3

Reorientation answered 11/4, 2018 at 12:58 Comment(1)
On RHEL8 / CentOS Stream release 8 install platform-python-develKersey
T
4

So far I've been using pip install 2to3. Conversions worked like absolute charm! (I'm on Ubuntu 18.04)

Triplenerved answered 5/4, 2020 at 9:15 Comment(0)
C
3

On Fedora 29, the python-tools is not providing 2to3. I had to install the development package:

dnf install python3-devel

I found this out by first calling:

dnf whatprovides /usr/bin/2to3
Cub answered 19/3, 2019 at 17:5 Comment(0)
G
0

Check if you have the file /usr/bin/2to3-2.7 (or similar). If one exists, then simply create a symbolic link to it with a link name of 2to3 (e.g.: ln -s 2to3-2.7 /usr/bin/2to3).

Gazelle answered 15/6, 2020 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.