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.

How to support both Python 2 and 3

84 pointsby tshepangalmost 12 years ago

8 comments

zzzeekalmost 12 years ago
this article takes awhile to get to the point, but the point is correct which is:<p>&gt; In other words, this is how it should be, that you write your code once, and you can use any supported language version to run it&#x2F;compile it, or develop in. But for some reason, this obvious solution has been discouraged by Guido and other Python documents, as seen above. I just looked up the latest official Python docs, and that one is not upfront negative about a single code base. But it still does not recommend this approach as the one. So let me fix that: I do recommend a single code base as the solution.<p>2to3 is definitely useful to do the initial conversion of a codebase from something totally 2.x into one that is 3.x compatible. But from that point on, you need to put it away and not look at it again. Now that it&#x27;s socially acceptable to target 2.6 at the lowest, libraries that wish to target 2.6-3.x still need to have a little bit of conversion code present, i.e. a subset of six, but other than that the code can be fairly py3k-ish.<p>Armin&#x27;s article here is the most up to date discussion of this: <a href="http://lucumr.pocoo.org/2013/5/21/porting-to-python-3-redux/" rel="nofollow">http:&#x2F;&#x2F;lucumr.pocoo.org&#x2F;2013&#x2F;5&#x2F;21&#x2F;porting-to-python-3-redux&#x2F;</a> (though lots of us disagree with his decision to dump 3.1&#x2F;3.2). This article has lots of <i>great</i> advice like &quot;test for py2k not 3k&quot; as well as his metaclass decorator that is a lot nicer than six&#x27;s. SQLAlchemy&#x27;s second py3k port (yes, due to the original advice we all have had to port to python 3 <i>twice</i>) makes heavy use of these techniques.<p>At this point I have <i>all</i> of my major codebases supporting 2.x-3.x. Particularly, Mako templates supports <i>python 2.4 all the way through 3.x with no conversion</i> - and Mako deals heavily with unicode and Python AST structures.<p>So hopefully the Python docs can catch up with the reality soon. The original advice to think very separately about your 2.x and 3.x codebases was very wrong, has wasted a ton of time (since you end up porting to python 3 twice), and overall slowed down the adoption of py3k, a slowdown that fortunately is finally lifting.
评论 #6158740 未加载
gus_massaalmost 12 years ago
I think that the ideas and tips of the article are useful, but I don&#x27;t like the opening phrase.<p>People make mistakes. Once a mistake is part of a language specification the only way to solve it is to make a backward incompatible change. It&#x27;s pain in the ass for the users, so you must not change this kind of changes too often if you like to have users.<p>A possible alternative is to have a huge metaprograming platform like Racket, that can still run the old version &quot;mzscheme&quot; programs almost unchanged [1]. But in the Racket&#x2F;Scheme&#x2F;Lisp word everyone is running their own slightly &quot;improved&quot; version of the language. So multiple languages are more natural than in Python with the &quot; There should be one-- and preferably only one --obvious way to do it.&quot; philosophy.<p>(For example, they changed allowed syntax of &quot;if&quot;.)
shadowmintalmost 12 years ago
The commit is more interesting than the article: <a href="https://github.com/sympy/sympy/pull/2318" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sympy&#x2F;sympy&#x2F;pull&#x2F;2318</a><p>It does fail to mention that you need to also use special exception handling in all cases, eg.<p><pre><code> try: ... except TypeError: e = exception() ... </code></pre> ...but it hits all the other things, except the obvious, make sure your tests are running continuously using py2, py3 and pypy, otherwise you&#x27;ll just frustrate yourself as you&#x27;re coding.
评论 #6156028 未加载
评论 #6155876 未加载
d0malmost 12 years ago
Call me crazy but I would have renamed Python3 to a new name, i.e. Snake. That would have made it crystal clear that it&#x27;s not the same language.<p>Seriously, Python3 made me hate python. A couple examples why:<p><pre><code> - The fact that I was using Archlinux and they decided to make &quot;default python&quot; -&gt; &quot;python3&quot; = Hours of wasted efforts figuring which scripts weren&#x27;t working. - Every time I tried to go with 3.x = Screwed because libraries I needed weren&#x27;t ported - print &#x27;x&#x27; not working = Just plain annoying </code></pre> Let&#x27;s make a new python4 == python2.7 and forget about all this python3 bullshit.
评论 #6156276 未加载
Demiurgealmost 12 years ago
I really love Python, it is my language of choice, bread and butter, and I&#x27;ve been using it since 2005. However, this backwards compatibility break is probably the worst thing that happened to the language, as I think it&#x27;s creating more project stalling and work in total than is worth any improvements specifically stemming from backwards incompatible changes. It could be because python3 still has not become my target, but that&#x27;s kind of the point. When is it ever going to be?
评论 #6169574 未加载
oh_sighalmost 12 years ago
What benefits does upgrading from python 2.x to 3 have? Why would you undertake that task? I don&#x27;t understand why, unless python 3 offers some extremely compelling feature, or you are one of the small percentage of developers who delivers importable python libraries as your main developmental output.
zdwalmost 12 years ago
I&#x27;m unclear how this is better than doing, as is mentioned in the first paragraph, a single code base that runs in both 2.7 and 3.x versions?<p>Or is this a case where 2.6 needs to be supported?
mortdeusalmost 12 years ago
You dont. Just support python 3. If you HAVE to support python 2. Then use a symbolic link.