(original) (raw)

On 22 July 2012 14:08, R. David Murray <rdmurray@bitdance.com> wrote:
On Sun, 22 Jul 2012 11:21:38 +0300, anatoly techtonik <techtonik@gmail.com> wrote:
> http://docs.python.org/py3k/howto/pyporting.html#during-installation
>
> What's the point in making implicit Python 3 check here:
> try: �# Python 3
> � from distutils.command.build\_py import build\_py\_2to3 as build\_py
> except ImportError: �# Python 2
> � from distutils.command.build\_py import build\_py
>
> instead of explicit check like:
> import sys
> if sys.version\_info\[0\] >= 3:
> � � from distutils.command.build\_py import build\_py\_2to3 as build\_py

It's called testing for the thing that actually matters, rather than
testing a constant with a much broader meaning. �Yes, in this case the
results are the same, but IMO it is better programming practice to test
the thing that actually matters when you can.

I recently changed a setup.py from try/ImportError to an explicit sys.version\_info check.�I'm not totally sure how to reproduce this but I had a problem where I was installing into a 2.x virtualenv and it was running 2to3 during install and subsequently failing to import the 3.x code (the problem didn't occur when using the same python that generated the virtualenv).

I may be wrong but I imagined that sometimes build\_py\_2to3 is importable on 2.x, perhaps for cross-building or something. In any case 'testing the thing that matters' means testing what version of Python you are about to install into not whether the python version supports running 2to3.

Cheers,
Oscar.