Eric Thomas
Eric Thomas

Reputation: 717

Dangers of updating from python 2.7 to 3.0 and above

A person for which I am working under constantly has reserves about me updating programs that were written in python 2.5 and 2.7 to python 3.3. I work in bioninformatics and a lot of the python code I am trying to work with is pre 3.0 and while I have a linux that runs 2.7 it on a virtual machine, on my main machine I am already at python 3.3 and develop my programs with it. I understand that if the program has heavy reliance on the libraries then there could be some compatibility issues but besides that I don't see why I can't just spend a little time upgrading it. I feel I should clarify that most of this programs are not much more then the a few hundred lines of code.

What I really want to know is;

Are there some real differences between the two version that might cause a program to run different?

Is it possible to just simply update to 3.3 and clean it up by just changing the over things like print to print() or urlib2 to the update urlib?

Upvotes: 0

Views: 329

Answers (3)

It is quite easy to write new code that works both python 2.6+ and 3.3+. Use the following at the beginning of each file

from __future__ import division, absolute_import \
        unicode_literals, print_function

And then know the differences; you can use the six to ease the porting. However be warned that many needed modules might be written for 2.x only (I suspect more so in the field of bioinformatics than in general programming) and it might not only be your code that needs porting. I'd expect you to still need the 2.x for some time. I'd advice against using the 2to3, to me the right way to proceed is to write code that works on both python 2.x and 3.x out of box - it eases the development and testing.

As for the old code, be warned that the str/unicode confusion will hit you hard many times - some python 2 str should be bytes, and some should be python 3 str.

Upvotes: 1

roippi
roippi

Reputation: 25954

As an aside: are you familiar with 2to3?

The list of things that can 'go wrong' is basically too large for this Q+A format. Here is what the docs have to say about porting code. The most vital point is to have well-written tests, but in academia I realize that's not always reality. In lieu of that, see the 'gotchas' section for the most common ones that automation will not/can not pick up.

The two that I see most in academic code are reliance on integer division and opening files in non-binary mode. Without knowing specifics, I'd say to watch out for those.

Upvotes: 0

user2555451
user2555451

Reputation:

I don't know if SO is the best place to ask this question, but this link here lists every difference between Python 2.x and 3.x: http://docs.python.org/3.0/whatsnew/3.0.html

After reading it, you can easily tell what needs to be done to your 2.x programs to bring them into 3.x.

Upvotes: 0

Related Questions