python 2.5 and ast

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Nov 29 06:32:30 EST 2011


On Tue, 29 Nov 2011 09:51:24 +0000, Andrea Crotti wrote:

> On 11/29/2011 03:55 AM, Dave Angel wrote:
>>
>> But don't forget to tag it as version specific, so it gets removed when
>> the later version of the library is available.  There are various ways
>> of doing that, but the easiest is probably to put a test in the
>> acceptance suite that fails if this code is used in 2.6 or later.
>>
>>
> Ok thanks,
> something like this is ok?
> (or maybe I can use a try/catch and export my own if is not found in the
> standard library?)
> 
> from sys import version_info
> 
> if version_info[1] == 5:
>      from psi.devsonly.ast import parse, NodeVisitor
> else:
>      from ast import parse, NodeVisitor


I prefer to check against sys.version.

import sys
if sys.version <= '2.5':
    from psi.devsonly.ast import parse, NodeVisitor
else:
    from ast import parse, NodeVisitor



Or even:


try:
    from ast import parse, NodeVisitor
except ImportError:
    from ast import parse, NodeVisitor



-- 
Steven



More information about the Python-list mailing list