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
  • 8,619
  • 7
  • 36
  • 40
  • 1
    `import `[`six`](http://pythonhosted.org/six/) – tripleee Oct 28 '14 at 06:28
  • 2
    See also http://stackoverflow.com/questions/11372190/python-2-and-python-3-dual-development – tripleee Oct 28 '14 at 06:29
  • @tripleee with six, I can write code that can run on python 2 and 3. But how can I make two dependence libs (one needs python2, the other needs python3) work together? – Jacky Oct 28 '14 at 06:41
  • I need my code work with gitdb which needs python2 and python-perforce which needs python3. – Jacky Oct 28 '14 at 06:42

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
  • 91,433
  • 48
  • 218
  • 260
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
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
  • 300,191
  • 65
  • 633
  • 696