0

I found that some libs depend python2 and some libs work on python3. I know there is a lib that can port code from python 2 to python 3. I am not sure if it has more easy way to make these libs work together. How can I use these libs? Thanks a lot

EDIT: to make the question more specific. I want to write python script to copy changelist from perforce and commit them into git. So I want to use both GitPython and P4Python. GitPython can only work on python 2 (although evaluate python 3.3 compatibility is its current goal) while P4Python can only work on python 3.

How can I make this work?

Jacky
  • 7,725
  • 7
  • 34
  • 39

2 Answers2

2

There are many tools and tricks to port python 2.x to python 3.x and to write code compatible with both version.

You will find a dedicated Howto on that subject in Python 3 documentation. Some referenced tools:

  • 2to3: to convert from Python2 to Python3
  • caniusepython3 (command-line tool, web app: to control dependencies
  • six: low level project to help writing 2-3 compatible code
  • modernize: to help porting existing code to six

And last but not least __future__ is a must in that context, at least the famous:

from __future__ import print_function

that allows using the print() function from Python3 under Python2

But if you want to do serious things, read the whole Howto.

Eric O Lebigot
  • 81,422
  • 40
  • 198
  • 249
Serge Ballesta
  • 121,548
  • 10
  • 94
  • 199
1

Most python2.x projects can be made to work with python3 using the 2to3 tool which typically comes bundled with your python3.x installation.

As noted in the comments, if you're interested in developing code that can be used (out of the box) on both python2.x and python3.x, the third party package six contains lots of useful utilities for writing backward and forward compatible code. It doesn't help you run somebody else's code though :-).

mgilson
  • 264,617
  • 51
  • 541
  • 636