Requiring a certain version of interpreter?

Andrew Dalke dalke at acm.org
Wed Jun 28 07:01:48 EDT 2000


Matthew Cline <matt at nightrealms.com> asked:
> Is there any builtin feature, or common module, that handles requiring
> at least a certain version of the Python interpreter to be used for a
> script?  Perl has 'require' for this purpose.


Chris Armstrong replied:
>>>> import sys
>>>> print sys.version
>1.5.2 (#0, Apr  3 2000, 14:46:48)  [GCC 2.95.2 20000313 (Debian GNU/Linux)]
>
>There may a way to just get the version number only, but if nothing else
this
>will work.

Python's Makefile.pre uses sys.version[:3] to get the version.  At the
very least, this implies there is no other way to get that data - I've
never seen another way.

The other answer is, why do you need this?  You should write your code
to check for capability, rather than version.  There are two major versions
of Python, one in C and the other in Java.  There's no guarantee that the
version numbers will be the same on both platforms and mean the same thing.

For example, suppose your code wants to use list extend, which was added
in (I think) 1.5.1.  You could force a check like:

if "extend" not in dir([]):
  raise ImportError, "This version of Python does not support list.extend()"

Perl doesn't have this problem because only one person has been, umm..,
as adventerous as Larry Wall to write a Perl implementation from scratch.

                    Andrew
                    dalke at acm.org







More information about the Python-list mailing list