You are not logged in.
Hi everyone,
I'm currently writing a Python library and I'm a bit lost in regard to compatibility. Here are many Python programmers, I hope somebody can help me.
I use strings heavily for byte data and unicode strings for texts. (code sample) Python 3 turns str into unicode strings and has a new type for byte data. The old unicode function doesn't exist anymore and causes an error.
What's the best way to achieve compatibility to both Python 2.5 (default in Debian and Ubuntu) and Python 3? Do I have to maintain two versions for the time being? Is there something like #ifdefs in C (this would fortunately only affect a small part of my code) I can use?
Thanks for your answers.
Offline
Use the python module 2to3
Offline
Yes, the Python devs recommend, if you're targetting both versions, that you write your code for Python 2.x and keep tweaking it until 2to3 converts it to Python3 without choking, and the resulting code runs under Python 3 without problems. But you always code in Python 2.x.
A different strategy, which you might combine with the previous, is to have some of your files check sys.version to decide whether to import myfoo3 or import myfoo2. You write Python 2.x-specific code in myfoo2.py and Python3-specific code in myfoo3.py.
Offline
Ah, thank you for your answers, particularly at Profjim for explaining the developmental strategy.
I'll have to drop Python 2.5 compatibility in order to properly use 2to3. I guess I'll wait until the next Ubuntu LTS and Debian Squeeze and then go with Python 2.6 + 2to3.
Offline
There is no way of having a source file that is both 2.x and 3.1 compatible, the two versions are incompatible. I suggest keeping seperate files for the two versions to avoid confusion. If you have to pick one version, 2.6 is your best bet, as it is the most widely used at the moment.
The sys.version method mentioned by Profjim sounds nice, I should try it somem time.
Offline
I'm not a programmer or anything but I've seen waf scripts simply use:
if sys.hexversion<hexvalue: raise ImportError("your error message") #to set minimum python 2 version
and
if sys.hexversion>hexvalue: #for python3 code
English is not my native language .
Offline