check interpreter version before running script

Discussion in 'Python' started by rbt, Apr 5, 2005.

  1. rbt

    rbt Guest

    Is there a recommended or 'Best Practices' way of checking the version
    of python before running scripts? I have scripts that use the os.walk()
    feature (introduced in 2.3) and users running 2.2 who get errors.
    Instead of telling them, 'Upgrade you Python Install, I'd like to use
    sys.version or some other way of checking before running.

    Whatever I do, I need it to work on Linux, Mac and Windows.

    I thought of sys.version... but getting info out of it seems awkward to
    me. First 5 chars are '2.4.1' in string format have to split it up and
    convert it to ints to do proper checking, etc. It doesn't seem that
    sys.version was built with this type of usage in mind. So, what is the
    *best* most correct way to go about this?

    Thanks,

    rbt
    rbt, Apr 5, 2005
    #1
    1. Advertising

  2. rbt

    Peter Otten Guest

    rbt wrote:

    > Is there a recommended or 'Best Practices' way of checking the version
    > of python before running scripts? I have scripts that use the os.walk()
    > feature (introduced in 2.3) and users running 2.2 who get errors.
    > Instead of telling them, 'Upgrade you Python Install, I'd like to use
    > sys.version or some other way of checking before running.


    I like

    import os

    try:
    os.walk
    except AttributeError:
    # implement fallback

    No need to remember in which version the desired feature came to be.

    Peter
    Peter Otten, Apr 5, 2005
    #2
    1. Advertising

  3. rbt

    F. Petitjean Guest

    Le Tue, 05 Apr 2005 08:57:12 -0400, rbt a écrit :
    > Is there a recommended or 'Best Practices' way of checking the version
    > of python before running scripts? I have scripts that use the os.walk()
    > feature (introduced in 2.3) and users running 2.2 who get errors.
    > Instead of telling them, 'Upgrade you Python Install, I'd like to use
    > sys.version or some other way of checking before running.
    >
    >
    > I thought of sys.version... but getting info out of it seems awkward to
    > me. First 5 chars are '2.4.1' in string format have to split it up and
    > convert it to ints to do proper checking, etc. It doesn't seem that
    > sys.version was built with this type of usage in mind. So, what is the
    > *best* most correct way to go about this?

    try:
    from os import walk as os_walk
    except ImportError:
    os_walk = None
    # raise some exception or implement a fallback solution
    # use os_walk instead of os.walk
    >
    > Thanks,
    >
    > rbt
    F. Petitjean, Apr 5, 2005
    #3
  4. rbt

    Peter Hansen Guest

    rbt wrote:
    > Is there a recommended or 'Best Practices' way of checking the version
    > of python before running scripts? I have scripts that use the os.walk()
    > feature (introduced in 2.3) and users running 2.2 who get errors.
    > Instead of telling them, 'Upgrade you Python Install, I'd like to use
    > sys.version or some other way of checking before running.
    >
    > Whatever I do, I need it to work on Linux, Mac and Windows.


    Why does this have to occur "before running the script"? Can't
    you just do it as the first thing the script does on startup?
    That is the usual Best Practice approach.

    > I thought of sys.version... but getting info out of it seems awkward to
    > me. First 5 chars are '2.4.1' in string format have to split it up and
    > convert it to ints to do proper checking, etc. It doesn't seem that
    > sys.version was built with this type of usage in mind. So, what is the
    > *best* most correct way to go about this?


    Use sys.version_info instead. As it's a tuple, you can
    just slice-and-dice as needed, and compare subsets of
    it with other tuples.

    -Peter
    Peter Hansen, Apr 5, 2005
    #4
  5. F. Petitjean wrote:
    > Le Tue, 05 Apr 2005 08:57:12 -0400, rbt a écrit :
    >
    >>Is there a recommended or 'Best Practices' way of checking the version
    >>of python before running scripts? I have scripts that use the os.walk()
    >>feature (introduced in 2.3) and users running 2.2 who get errors.
    >>Instead of telling them, 'Upgrade you Python Install, I'd like to use
    >>sys.version or some other way of checking before running.
    >>
    >>
    >>I thought of sys.version... but getting info out of it seems awkward to
    >>me. First 5 chars are '2.4.1' in string format have to split it up and
    >>convert it to ints to do proper checking, etc. It doesn't seem that
    >>sys.version was built with this type of usage in mind. So, what is the
    >>*best* most correct way to go about this?

    >
    > try:
    > from os import walk as os_walk
    > except ImportError:
    > os_walk = None
    > # raise some exception or implement a fallback solution
    > # use os_walk instead of os.walk
    >
    >>Thanks,
    >>
    >>rbt


    You may be interested in this...


    http://www.jorendorff.com/articles/python/path/


    as an alternative to the os.path functions...
    Martin
    Martin Franklin, Apr 5, 2005
    #5
  6. rbt

    Josef Meile Guest

    > Is there a recommended or 'Best Practices' way of checking the version
    > of python before running scripts? I have scripts that use the os.walk()
    > feature (introduced in 2.3) and users running 2.2 who get errors.
    > Instead of telling them, 'Upgrade you Python Install, I'd like to use
    > sys.version or some other way of checking before running.
    >
    > Whatever I do, I need it to work on Linux, Mac and Windows.
    >
    > I thought of sys.version... but getting info out of it seems awkward to
    > me. First 5 chars are '2.4.1' in string format have to split it up and
    > convert it to ints to do proper checking, etc. It doesn't seem that
    > sys.version was built with this type of usage in mind. So, what is the
    > *best* most correct way to go about this?
    >

    What's about:
    >>> import sys
    >>> print sys.version_info

    (2, 1, 3, 'final', 0)

    Regards,
    Josef
    Josef Meile, Apr 5, 2005
    #6
  7. rbt

    djo Guest

    Josef Meile wrote:
    > What's about:
    > >>> import sys
    > >>> print sys.version_info

    > (2, 1, 3, 'final', 0)


    Python 1.5.2 (#1, Mar 3 2001, 01:35:43) [GCC 2.96 20000731 (Red
    Hat Linux 7.1 2 on linux-i386
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>> import sys
    >>> sys.version_info

    Traceback (innermost last):
    File "<stdin>", line 1, in ?
    AttributeError: version_info


    ....which the OP may have known, since they suggested sys.version,
    which *is* availible in pre-2.0.


    My own messy solution involves a shell script installer which
    runs a 1.5-safe Python script which installs a needs-2.2 Python
    script. But I needed an installer for other reasons. This would
    be ugly for a single script.


    djo
    djo, Apr 5, 2005
    #7
  8. "rbt" <> wrote:

    > Is there a recommended or 'Best Practices' way of checking the version of python before running
    > scripts? I have scripts that use the os.walk() feature (introduced in 2.3) and users running 2.2
    > who get errors. Instead of telling them, 'Upgrade you Python Install, I'd like to use sys.version
    > or some other way of checking before running.


    if you depend on os.walk, check for os.walk.

    try:
    from os import walk
    except ImportError:
    print "sorry, you need a newer python version!"
    sys.exit()

    or

    import os
    try:
    os.walk
    except AttributeError:
    print "sorry, you need a newer python version!"
    sys.exit()

    if you only depend on a few functions, you can usually emulate them in
    earlier versions. if 2.2 or newer is a reasonable requirement, you can
    put a copy of the walk function in your script:

    from __future__ import generators
    try:
    from os import walk
    except ImportError:
    def walk(...):
    # copied from os.py in 2.3

    if you want to support e.g 1.5.2 or newer, you can use something like this:

    import os
    try:
    from os import walk
    except ImportError:
    class walk:
    def __init__(self, directory):
    self.stack = [directory]
    def __getitem__(self, index):
    dirpath = self.stack.pop(0)
    dirnames = []
    filenames = []
    for file in os.listdir(dirpath):
    name = os.path.join(dirpath, file)
    if os.path.isdir(name) and not os.path.islink(name):
    dirnames.append(file)
    self.stack.append(name)
    else:
    filenames.append(file)
    return dirpath, dirnames, filenames

    (tweak as necessary)

    </F>
    Fredrik Lundh, Apr 5, 2005
    #8
  9. rbt

    rbt Guest

    Peter Otten wrote:
    > rbt wrote:
    >
    >
    >>Is there a recommended or 'Best Practices' way of checking the version
    >>of python before running scripts? I have scripts that use the os.walk()
    >>feature (introduced in 2.3) and users running 2.2 who get errors.
    >>Instead of telling them, 'Upgrade you Python Install, I'd like to use
    >>sys.version or some other way of checking before running.

    >
    >
    > I like
    >
    > import os
    >
    > try:
    > os.walk
    > except AttributeError:
    > # implement fallback
    >
    > No need to remember in which version the desired feature came to be.
    >
    > Peter


    Thanks for all the tips. I found this tip from Peter the best for my
    situation.
    rbt, Apr 6, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Schulze

    Check from C that Interpreter is running

    Florian Schulze, Jul 22, 2003, in forum: Python
    Replies:
    1
    Views:
    299
    Michael Hudson
    Jul 22, 2003
  2. Raphael Zulliger
    Replies:
    3
    Views:
    596
    Raphael Zulliger
    May 26, 2005
  3. V Green
    Replies:
    0
    Views:
    809
    V Green
    Feb 5, 2008
  4. PA Bear [MS MVP]
    Replies:
    0
    Views:
    910
    PA Bear [MS MVP]
    Feb 5, 2008
  5. John Reid
    Replies:
    0
    Views:
    231
    John Reid
    Jul 27, 2010
Loading...

Share This Page