#!/usr/bin/env python > 2.4?

R

rh0dium

Hi Folks,

OK I love the logging module. I use it everywhere. I was happily
putting at the top of each of my scripts

------<snip>------
#!/usr/bin/env python2.4

import logging

LOGLEVEL=logging.INFO

# Set's up a basic logger
logging.basicConfig(level=LOGLEVEL, format="%(asctime)s %(name)s %
(levelname)-8s %(message)s",
datefmt='%d %b %Y %H:%M:%S', stream=sys.stderr)

try:
module= os.path.basename(traceback.extract_stack(limit=2)[1]
[0]).split(".py")[0]+"."
except:
module = os.path.basename(traceback.extract_stack(limit=2)[0]
[0]).split(".py")[0]+"."

def main(loglevel=False):
""" """
# Setup Logging
log = logging.getLogger(module+sys._getframe().f_code.co_name )
if loglevel is not False: log.setLevel(loglevel)
else:log.setLevel(logging.WARN)

log.debug("I LOVE LOGGING")

if __name__ == '__main__':
sys.exit(main(loglevel=LOGLEVEL))


------<snip>------

But now you guys continued to make this cool language and 2.5 came
out. Great now how do I put /usr/bin/env python>2.4?

Or can you suggest a better way to skin this cat?

Thanks again!!
 
S

Steve Holden

rh0dium said:
Hi Folks,

OK I love the logging module. I use it everywhere. I was happily
putting at the top of each of my scripts

------<snip>------
#!/usr/bin/env python2.4
[...]

But now you guys continued to make this cool language and 2.5 came
out. Great now how do I put /usr/bin/env python>2.4?

Or can you suggest a better way to skin this cat?

Thanks again!!
The usual way is something like

#!/usr/bin/env python

Python usually installs so the latest version gets linked as
/usr/bin/python. HTere's no need to bind your scripts to a particular
version.

regards
Steve
 
R

rh0dium

Python usually installs so the latest version gets linked as
/usr/bin/python. HTere's no need to bind your scripts to a particular
version.

regards

True - but that entirely depends on your path. Example:

Redhat (RHEL3) ships with python2.3 in /usr/bin
Adding an 2.5 version to /usr/local/bin

set PATH=/usr/local/bin:/usr/bin

"python" - will use 2.5

Conversely if you:
set PATH=/usr/bin:/usr/local/bin

"python" - will use 2.3

but if I wanted to ensure I was using 2.5 I would simply type
python2.5 I want to ensure that the python version I am using is at
lease 2.4
 
S

Stargaming

rh0dium said:
True - but that entirely depends on your path. Example:

Redhat (RHEL3) ships with python2.3 in /usr/bin
Adding an 2.5 version to /usr/local/bin

set PATH=/usr/local/bin:/usr/bin

"python" - will use 2.5

Conversely if you:
set PATH=/usr/bin:/usr/local/bin

"python" - will use 2.3

but if I wanted to ensure I was using 2.5 I would simply type
python2.5 I want to ensure that the python version I am using is at
lease 2.4

#!/usr/bin/env python

and

from sys import version_info
if version_info[0] < 2 or version_info[1] < 4:
raise RuntimeError("You need at least python2.4 to run this script")

IMO you shouldn't struggle with it too hard. If the user's python
version is not appropriate, don't hack its interpreter mechanism to do
the work you need. Anyways, you should not check for specific python
versions but for modules/APIs/builtins whatever and *then* you may raise
an exception pointing the user to the fact that is python version does
not fit your needs.

HTH,
Stargaming
 
R

rh0dium

from sys import version_info
if version_info[0] < 2 or version_info[1] < 4:
raise RuntimeError("You need at least python2.4 to run this script")

This is great!!
IMO you shouldn't struggle with it too hard. If the user's python
version is not appropriate, don't hack its interpreter mechanism to do
the work you need. Anyways, you should not check for specific python
versions but for modules/APIs/builtins whatever and *then* you may raise
an exception pointing the user to the fact that is python version does
not fit your needs.

I agree but where do you raise the exception? Quite frankly it's
silly to kill the whole app when a bunch of debug lines won't print
and shouldn't except by the developer..
In other words is what you propose the right way to solve this for
modules which were'nt included in older python releases? Or should I
be doing something different and if so what should I do? I like the
solution you give - it will work for everything I do - but is it smart
to retrofit this into all of my old code? What is the clean way to
solve this?

Just want to do this the right way.
 
J

Jon Ribbens

from sys import version_info
if version_info[0] < 2 or version_info[1] < 4:
raise RuntimeError("You need at least python2.4 to run this script")

That'll fail when the major version number is increased (i.e. Python 3.0).

You want:

if sys.hexversion < 0x020400f0:
... error ...
 
S

starGaming

from sys import version_info
if version_info[0] < 2 or version_info[1] < 4:
raise RuntimeError("You need at least python2.4 to run this script")

That'll fail when the major version number is increased (i.e. Python 3.0).

You want:

if sys.hexversion < 0x020400f0:
... error ...

Yes, it was intended to be and 'and' instead of an 'or'.
 
S

Sander Steffann

Hi,

Op 21-mrt-2007, om 20:41 heeft (e-mail address removed) het volgende
geschreven:
from sys import version_info
if version_info[0] < 2 or version_info[1] < 4:
raise RuntimeError("You need at least python2.4 to run this
script")

That'll fail when the major version number is increased (i.e.
Python 3.0).

You want:

if sys.hexversion < 0x020400f0:
... error ...

Yes, it was intended to be and 'and' instead of an 'or'.

If you make it an 'and' it will not raise the exception on version
like 1.5 or 2.3... If you realy want to use version_info, you'll have
to do something like:

if version_info[0] < 2 or (version_info[0] == 2 and version_info[1] <
4):
raise RuntimeError

- Sander
 
S

starGaming

Hi,

Op 21-mrt-2007, om 20:41 heeft (e-mail address removed) het volgende
geschreven:


from sys import version_info
if version_info[0] < 2 or version_info[1] < 4:
raise RuntimeError("You need at least python2.4 to run this
script")
That'll fail when the major version number is increased (i.e.
Python 3.0).
You want:
if sys.hexversion < 0x020400f0:
... error ...
Yes, it was intended to be and 'and' instead of an 'or'.

If you make it an 'and' it will not raise the exception on version
like 1.5 or 2.3... If you realy want to use version_info, you'll have
to do something like:

if version_info[0] < 2 or (version_info[0] == 2 and version_info[1] <
4):
raise RuntimeError

- Sander


I don't see any problem with::

if version_info[0] <= 2 and version_info[1] < 4:
raise RuntimeError()

Huh?
 
G

Gabriel Genellina

En Wed, 21 Mar 2007 07:07:20 -0300, Jon Ribbens
from sys import version_info
if version_info[0] < 2 or version_info[1] < 4:
raise RuntimeError("You need at least python2.4 to run this
script")

That'll fail when the major version number is increased (i.e. Python
3.0).

You want:

if sys.hexversion < 0x020400f0:
... error ...

(what means the final f0?) I find a lot easier to use (and read) this:

if sys.version_info < (2,4):
raise RuntimeError("You need at least python2.4...")

(Does the f0 account for the prereleases?)
 
G

Gabriel Genellina

En Wed, 21 Mar 2007 14:42:53 -0300, Jon Ribbens
Maybe you should suggest a patch to the Python documentation then ;-)

The formula I mentioned is the one suggested in the official Python
documentation ( http://docs.python.org/lib/module-sys.html#l2h-5143 )
as being the way to check the Python version.

Uh... I never thought it was an implied formula there - that F0 had to
come from 1.5 = 15 = 0xF.
I think it should be stated much more clearly.
 
J

Jon Ribbens

Uh... I never thought it was an implied formula there - that F0 had to
come from 1.5 = 15 = 0xF.
I think it should be stated much more clearly.

I'm not sure what you're saying. You are correct that the
documentation is rather vague. The 'f0' comes from 'final release'
I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top