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

174 questions
142
votes
2 answers

Unpickling a python 2 object with python 3

I'm wondering if there is a way to load an object that was pickled in Python 2.4, with Python 3.4. I've been running 2to3 on a large amount of company legacy code to get it up to date. Having done this, when running the file I get the following…
NDevox
  • 3,751
  • 4
  • 18
  • 32
63
votes
9 answers

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.
GhostFrag1
  • 845
  • 2
  • 11
  • 19
61
votes
5 answers

Why does Python 3 need dict.items to be wrapped with list()?

I'm using Python 3. I've just installed a Python IDE and I am curious about the following code warning: features = { ... } for k, v in features.items(): print("%s=%s" % (k, v)) Warning is: "For Python3 support should look like ...…
Dewfy
  • 21,895
  • 12
  • 66
  • 114
52
votes
7 answers

How are you planning on handling the migration to Python 3?

I'm sure this is a subject that's on most python developers' minds considering that Python 3 is coming out soon. Some questions to get us going in the right direction: Will you have a python 2 and python 3 version to be maintained concurrently or…
Jason Baker
  • 171,942
  • 122
  • 354
  • 501
34
votes
1 answer

TypeError: a bytes-like object is required, not 'str' - python 2 to 3

Hi I am having trouble with this error message. I am new to Python and this Python2 and Python3 is a hassle. I'm not sure what to do here, the error message is as shown below. Using Ticker: AAPL Traceback (most recent call last): File…
Lasheen Lartey
  • 367
  • 2
  • 4
  • 8
31
votes
4 answers

Sorting list by an attribute that can be None

I'm trying to sort a list of objects using my_list.sort(key=operator.attrgetter(attr_name)) but if any of the list items has attr = None instead of attr = 'whatever', then I get a TypeError: unorderable types: NoneType() < str() In Py2 it wasn't a…
AlexVhr
  • 1,726
  • 1
  • 16
  • 28
27
votes
5 answers

Python 2 and Python 3 dual development

I'm just starting a new Python project, and ideally I'd like to offer Python 2 and 3 support from the start, with minimal developmental overhead. My question is, what is the best way of doing this for brand new projects? I have come across projects…
Gustav Larsson
  • 7,289
  • 2
  • 28
  • 50
24
votes
2 answers

How to use 2to3 tool in windows?

I tried to modify the sintax using 2to3 tool by running command python C:\Python32\Tools\scripts\2to3.py neo4j.py and got the output When opening neo4j.py however I noticed there hasn't been anything changed. Below is the block of code where…
Niko Gamulin
  • 63,517
  • 91
  • 213
  • 274
18
votes
6 answers

How to use/install python 2to3?

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…
Kirbies
  • 657
  • 1
  • 8
  • 16
16
votes
1 answer

Attempted relative import in non-package (after 2to3)

After converting to Python 3.x using 2to3 (see my previous question), I get this error during the build: File "setup.py", line 28, in from . import mof_compiler ValueError: Attempted relative import in non-package The code: from .…
Remko
  • 6,606
  • 2
  • 27
  • 51
16
votes
4 answers

Why does 2to3 change mydict.keys() to list(mydict.keys())?

I'm looking at an output from 2to3 that includes this change: - for file_prefix in output.keys(): + for file_prefix in list(output.keys()): where output is a dictionary. What is the significance of this change? Why does 2to3…
Praxeolitic
  • 17,768
  • 12
  • 57
  • 109
14
votes
3 answers

How to fix print(( (double parentheses) after 2to3 conversion?

When migrating my project to Python 3 (2to3-3.7 -w -f print *), I observed that a lot of (but not all) print statements became print((...)), so these statements now print tuples instead of performing the expected behavior. I gather that if I'd used…
HaPsantran
  • 3,727
  • 5
  • 18
  • 34
13
votes
1 answer

Python 2to3 not changing files

I have a bunch of modules written in python 2.x and I need them to be in 3.x for them to work. I ran 2to3 on the entire folder, and it seemed to work fine, but when I looked at the files again they were the same. I have not used 2to3 and was…
carloabelli
  • 4,041
  • 3
  • 35
  • 64
11
votes
5 answers

For Python2 to Python3 code conversion, Which version of Python & Django best suited?

Currently I am working in big firm where we need to convert python2 old big Django project into python3 version so I have done lots of research related but still not able to find any perfect answer related to which version of Python and Django best…
Moon
  • 3,322
  • 1
  • 14
  • 46
11
votes
2 answers

2to3 says "No changes needed", then "files that need to be modified"

I run 2to3 -f all -f idioms -f buffer -f set_literal -f ws_comma foo.py Output: RefactoringTool: No changes to foo.py RefactoringTool: Files that need to be modified: RefactoringTool: foo.py Contents of foo.py: print("Hi") How do I interpret this…
user2201041
1
2 3
11 12