Questions tagged [python-2to3]

2to3 is a tool for automated Python 2 to 3 code translation. Don't use this tag to ask about differences between Python 2 and Python 3. Use the [python-3.x] and [python-2.7] tags for that.

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. 2to3 supporting library lib2to3 is, however, a flexible and generic library, so it is possible to write your own fixers for 2to3. lib2to3 could also be adapted to custom applications in which Python code needs to be edited automatically.

For more information, see the documentation at: http://docs.python.org/library/2to3.html

185 questions
5
votes
1 answer

How to use 2to3 to translate all files in a directory

I'm able to translate one file. But I don't understand how to translate whole directory. From docs: To translate an entire project from one directory tree to another use: $ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode Can…
Dork
  • 1,816
  • 9
  • 29
  • 57
5
votes
3 answers

2to3 - how to keep newline characters from input file?

I'm attempting to run 2to3 on Windows machine where *.py files has Unix-style end-line characters. Running 2to3 modifies newline characters in output file. MCVE: print2.py content before print "Hello, world!"\n Executed command: 2to3 print2.py -w…
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
5
votes
1 answer

eval(input()) in python 2to3

From the Python 2to3 doc: input: Converts input(prompt) to eval(input(prompt)) I am currently trying to learn Python 3 after a few years working with Python 2. Can anybody please explain why the tool inserts eval before the call to input, and…
rahmu
  • 5,708
  • 9
  • 39
  • 57
4
votes
1 answer

Porting cython files from python2 to python3 with 2to3

I have a python package which was developed under python2.7, but I need to port it to python3.6 . I use cython in some parts of the code, hence the package has both .py and .pyx files. I tried the 2to3 command, but I got an error that I couldn't…
MMFF
  • 41
  • 3
4
votes
1 answer

How to use 2to3 to edit files in-place?

I am able to load the 2to3 file in Terminal. It does a lot of printing, with output like the following: - print str + print(str) ... But the file is left without changes. And I don’t have any instructions after “run 2to3”. Please help.
aagorobets
  • 51
  • 1
  • 2
4
votes
1 answer

Converting Python2 to Python3 with metaclasses resulted in a wrong flow

I have a very large Python 2.7.6 project which I need to convert to Python 3.4. I used 2to3 script but 'metaclass' processing seems to be broken. I filtered the code to shorten and pinpoint the problem. The following fragment works well with Python…
OGP
  • 950
  • 2
  • 11
  • 26
4
votes
2 answers

use/run python's 2to3 as or like a unittest

I have used the 2to3 utility to convert code from the command line. What I would like to do is run it basically as a unittest. Even if it tests the file rather than parts(functions, methods...) as would be normal for a unittest. It does not need to…
Vincent
  • 1,579
  • 4
  • 23
  • 38
4
votes
3 answers

Python 2to3 tool adds a vowel to my integer

I was running the 2to3 tool on various scripts I'd written to get an idea of what I will need to change to port these to Python 3 (though I will be doing it by hand in the end). While doing so, I ran into an odd change 2to3 made in one of my…
Soviero
  • 1,774
  • 2
  • 13
  • 23
4
votes
1 answer

Error Remains After Running 2to3.py on a Module

I used the 2to3.py script to convert several of my files to Python 3 a while back. I believe I need to run all fixers, so my command included -f all -f buffer -f idioms -f set_literal -f ws_comma -w I tried to run my converted code with Python 3,…
Matthew Stamy
  • 1,114
  • 1
  • 7
  • 9
4
votes
1 answer

Tool like 2to3, except for merges

I maintain a fork of my project for Python 3.1. When I initially made the port from 2.6, I used 2to3, but now I constantly have to merge new code from the 2.6 fork into the 3.1 fork. How can I perform the 2to3 operation on these merges…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
3
votes
3 answers

Python 2to3 not working

I'm currently going through the python challenge, and i'm up to level 4, see here I have only been learning python for a few months, and i'm trying to learn python 3 over 2.x so far so good, except when i use this bit of code, here's the python 2.x…
bk201
  • 346
  • 2
  • 5
  • 14
3
votes
1 answer

Library to parse Python 2 and 3

I'm looking for a library that allows parsing and modification of Python 3 source code. There is the built-in ast module, but that doesn't allow parsing Python 3 code from Python 2 and vice versa. Is there such a library, or a way to make the ast…
phihag
  • 278,196
  • 72
  • 453
  • 469
3
votes
2 answers

Python 2to3 - do not remove unicode prefixes

I am converting a legacy codebase to python3 and do some dry runs of 2to3. 2to3 removes the u'' prefix from unicode literals creating a lot of noise in the diffs. Is there a way to disable this (as u'my string' is valid py3 syntax)?
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
3
votes
2 answers

How to handle strings transparently across pythons 2 and 3 without external modules?

What would be the simplest approach to universal string handling that would work in both python2 and python3 without having to use third-party modules like six? I am ok with using if sys.version_info > (3, 0)... but can't come up with a way to…
ccpizza
  • 28,968
  • 18
  • 162
  • 169
3
votes
2 answers

Python 2to3 Script Not Working - Unicode Error

I found a repo with lots of Python2 files that includes a script to convert them to Python 3. However I get the following error when I run it: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: truncated…
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
1 2
3
12 13