TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Porting to Python 3 Redux

103 pointsby dousalmost 12 years ago

6 comments

aidosalmost 12 years ago
It's great to see Armin's continued work on this. Things have obviously progressed since he wrote his post on the subject [0] (over a year ago). He's written so much of the code I rely on daily that I was concerned that he'd lose interest in Python during the 2-3 transition (I know it's not completely on him to support it but he is a key contributor to Python's use on the web).<p>[0] <a href="http://lucumr.pocoo.org/2011/12/7/thoughts-on-python3/" rel="nofollow">http://lucumr.pocoo.org/2011/12/7/thoughts-on-python3/</a>
kmike84almost 12 years ago
Great writeup (and a cool metaclass workaround!)<p>However, I think that "Drop 2.5, 3.1 and 3.2" advice is bad - dropping 2.5 and 3.1 is the way to go (hey, drop 2.5 even if you're not porting to 3.x), but dropping 3.2 is not necessary in most cases.<p>In my experience (porting and maintaining 20 open-source packages that work with Python 2 and Python 3 using a single codebase, including NLTK) Python 3.2 has never been a problem - I don't see how NLTK code and code of my other packages could be improved by dropping Python 3.2 compatibility.<p>The main argument for dropping Python 3.2 support seems to be that u'strings' are not supported in Python 3.2. There are 3 "types" of strings in Python:<p>* b"bytes",<p>* "native strings" # bytes in 2.x and unicode in 3.x<p>* u"unicode"<p>By adding `from __future__ import unicode_literals`` line to a top of the file, code compatible with 2.6-3.2 could be written like this:<p>* b"bytes"<p>* str("native string") # bytes in 2.x and unicode in 3.x<p>* "unicode"<p>In my opinion this is not a hack (unlike six.b and six.u necessary for 2.5 support), and this is arguably closer to Python 3.x semantics (unicode strings are default). So IMHO while using u"unicode" feature from Python 3.3 makes porting somewhat easier (less stupid search-replace), it also could make code worse and more cluttered, and Python 3.2 - compatible syntax is just fine.<p>It is true that 3.3 brings other improvements (Armin mentioned binary codecs), but it is quite rare that the library actually needs them (even libraries as big as NLTK and Django are fine with 3.2 stdlib).<p>3.2 is a default 3.x Python in current Ubuntu LTS (EOL in 2017) and a default 3.x Python in the recently released Debian Wheezy; 3.2 will be around for a long time, and not supporting it will hurt. So if you're doing Python 3.x porting, please just fix those stupid u'strings' with unicode_literals future import - your code will be more ideomatic and also 3.2 compatible.<p>There is also an advice for encoding __repr__ and __str__ results to utf8 under Python 2.x in the article; this is fine (other approaches are not better), but it has some non-obvious consequences (like breaking REPL in some setups) that developers should be aware of, see <a href="http://kmike.ru/python-with-strings-attached/" rel="nofollow">http://kmike.ru/python-with-strings-attached/</a><p>For lower-level 2.x-3.x compatible C/C++ extensions Cython is great. In fact, many libraries (e.g. lxml) are compatible with Python 3.x because they are written in Cython which generates compatible code (modulo library changes) by default.
评论 #5743438 未加载
评论 #5743424 未加载
wallunitalmost 12 years ago
&#62; If you have a C module written on top of the Python C API: shoot yourself. There is no tooling available for that yet from what I know and so much stuff changed.<p>I don't agree with that one. I have added Python 3 support two years ago to the Python bindings for libssh2 and it was straight forward. First of all it is still C and therefore you don't have to care about the syntax changed in Python. Just add some #if PY_MAJOR_VERSION &#60; 3 for API calls that have changed, or even better wrap that code in macros. Probably you already have some backwards compatibility switches/macros like that already anyway in your code, if you already support multiple versions of 2.x. So adding some more for Python 3, isn't that a big deal.<p>At least at that time, when six and modernizer wasn't available it was way easier and straight forward to support Python 2 and 3 with the same codebase with extension modules, than with actual Python code. And it seems if you don't want to drop support for Python &#60;= 2.5 or &#60;= 3.2, it still is.
评论 #5749395 未加载
评论 #5745036 未加载
评论 #5743462 未加载
mynegationalmost 12 years ago
I also found that maintaining single code base for both 2 and 3 is the only sane way. Running 2to3 during build is just too intrusive.<p>I liked the approach of 'six', but it is not shipped as a system module. Having something like that as a default system module in Python 2.6, 2.7, and 3.x would go a long way towards adoption of Python 3.<p>I found that I end up either using six or implementing some subset of it if I do not want to introduce the dependency.
bdarnellalmost 12 years ago
This is a good writeup. On Tornado I went through a similar transition from 2to3 to a single codebase. As long as you can drop Python 2.5 support you can probably avoid 2to3, but if you do need it I wrote some tools to make it less painful: <a href="http://bdarnell.github.io/blog/2012/03/13/cross-python-development-with-auto2to3/" rel="nofollow">http://bdarnell.github.io/blog/2012/03/13/cross-python-devel...</a>
hoodoofalmost 12 years ago
So Armin what did not come across from your blog post is how you <i>feel</i> about Python 3.<p>Until now you have been seen as one of the people holding out strongly against it. Where are you at now? Are you going to move to Python 3?<p>What's the future for Jinja2 now? Are you enthused about maintaining it? If it's headed for the deadpool please let us know as we can move to technologies that are going to have a future.<p>What the future for Flask?<p>What are your current thoughts on Python 3?