Versioning Libraries

Peter Hansen peter at engcorp.com
Thu Dec 2 20:38:15 EST 2004


Randall Smith wrote:
> As Python changes and old code still needs to work properly, I wonder if 
> there is a standard way to note which version of the Python interpreter 
> code is intended to work with.  I know that an executable may begin with 
> #!/usr/bin/python2.3 or something similar, but what about libraries and 
> such?  Would it be a good idea for the software I write to check for the 
> version of the interpreter?

Python is exceptionally backwards compatible, so generally
code from an older version will run unchanged on newer
Pythons.

There is a simple way of encoding a version of the interpreter,
but the real question is why would you want to do that.  If
you really think it's necessary, just import sys and check the
value of sys.version_info.  Lots of code which wants to require
a *minimum* version of Python (a far more common use case than
checking for a *maximum*) contains code like this:

import sys
if sys.hex_version[:3] < (2, 3, 3):
     sys.exit('Requires Python 2.3.3 or later to run!')


-Peter



More information about the Python-list mailing list