I have to convert some of my python 3 files to 2 for class, but I can't figure out how to use 3to2. I did pip install 3to2
and it said it was successful. It install 2 folders 3to2-1.1.1.dist-info and lib3to2. I have tried doing python 3to2 file_name
, `python lib3to2 file_name' I also tried changing the folder to 3to2.py like I saw on an answer on someone else question still didn't work. What is the correct way to use this?
-
1`python -m 3to2 file_name`, perhaps. – Peter Wood Dec 08 '15 at 23:34
-
@PeterWood I get no module named 3to2 should I change the lib3to2 folder to 3to2.py again? – Dec 08 '15 at 23:42
-
1`python /path/to/python/Scripts/3to2` – Peter Wood Dec 09 '15 at 00:17
-
1@PeterWood When I do the above it says `no such file or directory` I tried doing it again, but instead of 3to2 i did lib3to2 and it said `C:\Python34\python.exe: can't find '__main__' module in 'C:\\Python34\\Lib\\site-packages\\lib3to2'` – Dec 09 '15 at 16:01
4 Answers
Had the same question and here's how I solved it:
- pip install 3to2
- rename 3to2 to 3to2.py (found in the Scripts folder of the Python directory)
- Open a terminal window and run
3to2.py -w [file]
NB: You will either have to be in the same folder as 3to2.py or provide the full path to it when you try to run it. Same goes for the path to the file you want to convert. The easy way around this is to copy 3to2.py into the folder your py file is in and just run the command inside that folder. Use 3to2.py --help
for info on how the script works.

- 311
- 3
- 10
-
5There's absolutely no need to rename the file! just use `3to2 -w file`! Also you should add the Scripts folder to the PATH so that you can launch it independently of the working directory. – Bakuriu Jul 19 '16 at 11:40
-
1@Bakuriu You're right if we're talking Linux. Not if we're talking Windows. Windows does not support the shebang line, ie it has no way of knowing what to do with the script without an extension. – RolfBly Oct 04 '17 at 20:00
-
@RolfBly So? There is no point in renaming the file when you can just do `python 3to2 <...>` – Bakuriu Oct 04 '17 at 20:09
-
@Bakuriu If, in your first comment, you meant "just use `python 3to2 -w file`", then you're right. But it's not obvious that you did. – RolfBly Oct 05 '17 at 18:39
-
I don't think `3to2` supports converting collections classes like: `collections.Collection`, they still exist as-is though several other helpful conversions were made. – ecoe Dec 06 '18 at 18:50
A note for linux users:
On linux, 3to2 is intended to be run as a standalone script. That is, all you need to do is run:
3to2 <file_name>
from the command line. That is, on linux, there's no need to run:
python 3to2 <file_name>

- 1,684
- 1
- 11
- 19
-
This is true for Linux only. In Windows, you can't run a script that has no extension (i.e. Windows doesn't support the shebang line). – RolfBly Oct 04 '17 at 19:58
-
@RolfBly, thanks for pointing that out. I should have clarified. The questioner is clearly operating in a Windows environment. – Chris Larson Oct 05 '17 at 20:15
In MacOS, I have anaconda package manager installed, so after pip install 3to2
I found executable at /Users/<username>/anaconda3/bin/3to2
Run ./3to2
to convert stdin
(-
), files or directories given as arguments. By default, the tool outputs a unified diff-formatted patch on standard output and a “what was changed” summary on standard error, but the -w
option can be given to write back converted files, creating .bak
-named backup files.
In Windows its in C:\Python27\Scripts\
as a file 3to2
Run by invoking python python 3to2 <filetoconvert>
to display the diff on console or with -w
option to write back the converted to same file.
I'm usually using python2.7, but found a package I want to use that only works with python 3.
Here is what I did, using MacOS:
mkvirtualenv --python=`which python3` python3-3to2 # Create a new virtual env using mkvirtualenv which use python 3 (mkvirtualenv was already installed)
pip install 3to2
which 3to2 # /usr/local/bin/3to2
/usr/local/bin/3to2 -w metabase/metabase.py
This updated the metabase/metabase.py
(converted to python 2) and created a metabase/metabase.py.bak
(original file in python 3)
Note: Python 3 is a requirement for 3to2 to work, see "Usage" section:
This branch of 3to2 must be run with Python 3.
Resources:
- https://stackoverflow.com/a/24119306/2391795
- https://pypi.org/project/3to2/ (their doc could use some love indeed, wasn't so obvious)

- 16,593
- 24
- 118
- 215