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
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
  • 4,014
  • 3
  • 30
  • 66
11
votes
2 answers

Python 2.7 warning: __init__ not compatible to __new__

I have a class defined in Python 2.7 like this: from future.builtins import object class Point(object): def __init__(self, x, y): self.x = x self.y = y In PyCharm, this gives a warning in the __init__ line: Signature is not…
knub
  • 3,892
  • 8
  • 38
  • 63
11
votes
1 answer

Which 2to3 fixers output valid Python 2 code?

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 Consider the forty fixers listed at https://docs.python.org/3/library/2to3.html#fixers . By design, they all…
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
9
votes
1 answer

Python 2to3 adding extra parenthesis around functional argument

I'm a bit baffled why 2to3 is bothering embracing my print arguments that are already in functional style to be wrapped in an extra set of parenthesis. For example print("\t[Warn] Can not connect {}".format(ssid)) becomes print(("\t[Warn] Can not…
jxramos
  • 7,356
  • 6
  • 57
  • 105
8
votes
2 answers

Python 3 writing to a pipe

I'm trying to write some code to put data into a pipe, and I'd like the solution to be python 2.6+ and 3.x compatible. Example: from __future__ import print_function import subprocess import sys if(sys.version_info > (3,0)): print ("using…
mgilson
  • 300,191
  • 65
  • 633
  • 696
7
votes
2 answers

Should we use pandas.compat.StringIO or Python 2/3 StringIO?

StringIO is the file-like string buffer object we use when reading pandas dataframe from text, e.g. "How to create a Pandas DataFrame from a string?" Which of these two imports should we use for StringIO (within pandas)? This is a long-running…
smci
  • 32,567
  • 20
  • 113
  • 146
7
votes
2 answers

How to fix ".dist-info directory not found" in my package?

I have a Python 2 package that I'm trying to upgrade to Python 3. It was written by someone who used to work on the same team I'm on now but who is no longer with the company, and unfortunately nobody left on the team is able to help out. After…
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
7
votes
1 answer

Tests not being convered by 2to3 in setup.py?

I have a setup.py that needs to support both Python 2 and 3. The code currently works and is installable in Python 2.x If I add the use_2to3 = True clause to my setup.py, then the module can be installed in Python 3, however, doing a: python…
Adam Parkin
  • 17,891
  • 17
  • 66
  • 87
6
votes
2 answers

Can you make Python3 give an error when comparing strings to bytes

When converting code from Python 2 to Python 3 one issue is that the behaviour when testing strings and bytes for equality has changed. For example: foo = b'foo' if foo == 'foo': print("They match!") prints nothing on Python 3 and "They match!"…
Carcophan
  • 1,508
  • 2
  • 18
  • 38
6
votes
1 answer

pattern to match 1-or-2-arg function call for lib2to3

I have a script that can re-write a Python module so that all occurrences of func(a) are transfored to func2(a is None). I now want to support also func(a, msg) becoming func2(a is None, msg), but I can't find the pattern that will do this. This…
Oliver
  • 27,510
  • 9
  • 72
  • 103
6
votes
1 answer

2to3 makes multiple imports

I'm writing python2 code that will be be portable for python3 as well (by running 2to3 during user installation). But 2to3 sometimes makes multiple imports: -import urlparse -import urllib -import urllib2 +import urllib.parse +import urllib.request,…
iTayb
  • 12,373
  • 24
  • 81
  • 135
5
votes
4 answers

RefactoringTool: ParseError: bad input: type=22, value='='

I'm refactoring some python2 code and changing it to python3 using 2to3 module. I've received following parse error: RefactoringTool: There was 1 error: RefactoringTool: Can't parse ./helpers/repo.py: ParseError: bad input: type=22, value='=',…
cmd
  • 63
  • 1
  • 4
5
votes
2 answers

How to write unicode text to file in python 2 & 3 using same code?

I am trying to write a program that can run through both python 2 & 3. It reads character from website and writes into file. I have already imported unicode_literals from __future__. Straight out trying to write a string that looks like this: txt =…
James T
  • 55
  • 2
  • 6
5
votes
1 answer

Porting a sub-class of python2 'file' class to python3

I have a legacy code that calls class TiffFile(file). What is the python3 way to call it? I tried to replace following in python2: class TiffFile(file): def __init__(self, path): file.__init__(self, path, 'r+b') By this in…
Dima Lituiev
  • 12,544
  • 10
  • 41
  • 58
5
votes
2 answers

BeautifulSoup4 can't be installed in python3.5 on Windows7

I have downloaded beautifulsoup4-4.5.3.tar.gz from https://www.crummy.com/software/BeautifulSoup/bs4/download/4.5/ and unzipped it to my python work directory(which is not my python install directory). However, when I run from bs4 import…
Harry
  • 299
  • 2
  • 5
  • 14
1
2
3
12 13