0

I have a python 3 project, and would like to add support for python 2. There is the 3to2 conversion utility, but its output is python 2. I want to support both 2 and 3. I know the six library supports this. But I'm wondering if there is a conversion tool or something like that which would convert from python 3 to using the six library (or, if there is some other not-totally-manual way to achieve this goal)?

Ry-
  • 218,210
  • 55
  • 464
  • 476
caleb
  • 2,687
  • 30
  • 25
  • 4
    There's not really anything good. Do it manually. And better to use [python-future](https://python-future.org) as the compat layer, because `six` is pretty crappy. – wim Oct 17 '18 at 22:35
  • 1
    There are going to be limits; `six` can't fix raw syntax differences, so if you used any syntax features of Py3 that don't exist in Py2 even with `__future__` imports, you'd need to run at least some of the `3to2` fixers. – ShadowRanger Oct 17 '18 at 22:36
  • My recommendation is to do it manually. Find an editor that can do find and replace on many files at once, and go down [this](http://python-future.org/compatible_idioms.html) list of differences between the languages. I've never had great luck with automated tools unless I wrote them ;) – Aaron Oct 17 '18 at 22:45

1 Answers1

1

Thanks from the suggestions in the comments, I looked into python-future and found that it provides a conversion tool from python 3 to python2/3 (with the supporting python-future library) called pasteurize. It also provides a similar tool to convert from python 2 to python2/3 called futurize.

I tested pasteurize, and it worked for my project. Some cleanup may be helpful after running either of these tools, but they certainly seem to provide a good first cut at the problem.

caleb
  • 2,687
  • 30
  • 25