473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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!!

Mar 20 '07 #1
15 2206
rh0dium wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Mar 20 '07 #2
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

Mar 20 '07 #3
rh0dium schrieb:
>>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
#!/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
Mar 20 '07 #4
On Mar 20, 12:30 pm, Stargaming <stargam...@gmail.comwrote:
>
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.

Mar 20 '07 #5
In article <et***********@ulysses.news.tiscali.de>, Stargaming wrote:
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 ...
Mar 21 '07 #6
Jon Ribbens <jo********@unequivocal.co.ukwrote:
>You want:

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

"Readability counts."

if sys.version_info < (2, 4):
... error ...

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Mar 21 '07 #7
In article <4R*******@news.chiark.greenend.org.uk>, Sion Arrowsmith wrote:
Jon Ribbens <jo********@unequivocal.co.ukwrote:
> if sys.hexversion < 0x020400f0:
... error ...

"Readability counts."

if sys.version_info < (2, 4):
... error ...
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.
Mar 21 '07 #8
On Mar 21, 11:07 am, Jon Ribbens <jon+use...@unequivocal.co.ukwrote:
In article <etpcon$1g8...@ulysses.news.tiscali.de>, Stargaming wrote:
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'.

Mar 21 '07 #9
Hi,

Op 21-mrt-2007, om 20:41 heeft st********@gmail.com het volgende
geschreven:
On Mar 21, 11:07 am, Jon Ribbens <jon+use...@unequivocal.co.ukwrote:
>In article <etpcon$1g8...@ulysses.news.tiscali.de>, Stargaming wrote:
>>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

Mar 21 '07 #10
On Mar 21, 11:11 pm, Sander Steffann <s.steff...@computel.nlwrote:
Hi,

Op 21-mrt-2007, om 20:41 heeft starGam...@gmail.com het volgende
geschreven:
On Mar 21, 11:07 am, Jon Ribbens <jon+use...@unequivocal.co.ukwrote:
In article <etpcon$1g8...@ulysses.news.tiscali.de>, Stargaming wrote:
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?

Mar 22 '07 #11
In article <11**********************@b75g2000hsg.googlegroups .com>, st********@gmail.com wrote:
I don't see any problem with::

if version_info[0] <= 2 and version_info[1] < 4:
raise RuntimeError()
What if the version number is 1.5?
Mar 22 '07 #12
On Mar 22, 5:23 pm, Jon Ribbens <jon+use...@unequivocal.co.ukwrote:
In article <1174580529.081658.326...@b75g2000hsg.googlegroups .com>, starGam...@gmail.com wrote:
I don't see any problem with::
if version_info[0] <= 2 and version_info[1] < 4:
raise RuntimeError()

What if the version number is 1.5?
Ah, now I get your problem. OK.

Mar 22 '07 #13
En Wed, 21 Mar 2007 07:07:20 -0300, Jon Ribbens
<jo********@unequivocal.co.ukescribió:
In article <et***********@ulysses.news.tiscali.de>, Stargaming wrote:
>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?)
--
Gabriel Genellina

Mar 23 '07 #14
En Wed, 21 Mar 2007 14:42:53 -0300, Jon Ribbens
<jo********@unequivocal.co.ukescribió:
In article <4R*******@news.chiark.greenend.org.uk>, Sion Arrowsmith
wrote:
>Jon Ribbens <jo********@unequivocal.co.ukwrote:
>> if sys.hexversion < 0x020400f0:
... error ...

"Readability counts."

if sys.version_info < (2, 4):
... error ...

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.

--
Gabriel Genellina

Mar 23 '07 #15
In article <ma***************************************@python. org>, Gabriel Genellina wrote:
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.
Mar 23 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rene Pijlman | last post by:
I can't seem to find any way to specify the character encoding with the DB API implementation of PyPgSQL. There is no mention of encoding and Unicode in the DB API v2.0 spec and the PyPgSQL README....
2
by: leroybt.rm | last post by:
I don't understand why this does not work: <FILE1> test1.py #Import Packages import string # data=0 data=data+1
5
by: Robert Brewer | last post by:
WARNING: The following is NOT all Pythonic. However, it is Python. This is just fun--nothing I'm going to put into production. I don't have any questions or problems--just thought I'd share. ...
1
by: Michael R Seefelt | last post by:
I have written a simple C-program that loads a Python Function that connects to a PostgreSQL database> When I only load and execute the Python function all works OK. If I call the Python function a...
5
by: john | last post by:
Hi I am devlopeing a data centric intranetsite with data in mysql database I would like your opinion on this architecture Data will be fetched from database by python & converted into xml The...
20
by: Shawn Milo | last post by:
I'm new to Python and fairly experienced in Perl, although that experience is limited to the things I use daily. I wrote the same script in both Perl and Python, and the output is identical. The...
8
by: js | last post by:
Hi list. If I'm not mistaken, in python, there's no standard library to convert html entities, like &amp; or &gt; into their applicable characters. htmlentitydefs provides maps that helps this...
4
by: pdlemper | last post by:
Have carefully installed Python 2.5.1 under XP in dir E:\python25 . ran set path = %path% ; E:\python25 Python interactive mode works fine for simple arithmetic . Then tried >> import math Get...
17
by: Johannes Bauer | last post by:
Hello group, I'm porting some code of mine to Python 3. One class has the __cmp__ operator overloaded, but comparison doesn't seem to work anymore with that: Traceback (most recent call last):...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.