How to specify Python version in script?

Discussion in 'Python' started by kj, Nov 11, 2009.

  1. kj

    kj Guest

    I have a script that must be run with Python 2.6.x. If one tries
    to run it with, say, 2.5.x, *eventually* it runs into problems and
    crashes. (The failure is quicker if one attempts to run it with
    Python 3.x.)

    Is there some way to specify at the very beginning of the script
    the acceptable range of Python versions?

    TIA!

    kynn

    P.S. I know that I can hardcode the path to a specific intpreter
    in the #! line, but I'm trying to keep the code a bit more general
    than that.
     
    kj, Nov 11, 2009
    #1
    1. Advertising

  2. On Wed, Nov 11, 2009 at 12:16 PM, kj <> wrote:
    >
    >
    >
    >
    > I have a script that must be run with Python 2.6.x.  If one tries
    > to run it with, say, 2.5.x, *eventually* it runs into problems and
    > crashes.  (The failure is quicker if one attempts to run it with
    > Python 3.x.)
    >
    > Is there some way to specify at the very beginning of the script
    > the acceptable range of Python versions?
    >
    > TIA!
    >
    > kynn
    >
    > P.S. I know that I can hardcode the path to a specific intpreter
    > in the #! line, but I'm trying to keep the code a bit more general
    > than that.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >

    sys.version ? sys.version_info ?
     
    David Robinow, Nov 11, 2009
    #2
    1. Advertising

  3. On Wed, Nov 11, 2009 at 12:16 PM, kj <> wrote:
    >
    >
    >
    >
    > I have a script that must be run with Python 2.6.x.  If one tries
    > to run it with, say, 2.5.x, *eventually* it runs into problems and
    > crashes.  (The failure is quicker if one attempts to run it with
    > Python 3.x.)
    >
    > Is there some way to specify at the very beginning of the script
    > the acceptable range of Python versions?
    >


    min_version = (2,6)
    import sys
    if sys.version_info < min_version :
    print >> stderr, "must be run with at least Python 2.6"
    sys.exit(1)


    > TIA!
    >
    > kynn
    >
    > P.S. I know that I can hardcode the path to a specific intpreter
    > in the #! line, but I'm trying to keep the code a bit more general
    > than that.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Benjamin Kaplan, Nov 11, 2009
    #3
  4. Hello,

    If you are working on linux, you can change the shebang line from:
    #!/usr/bin/python

    to:
    #!/usr/bin/python2.6

    Best regards,
    Javier

    P.S. If you just want to avoid python 3 while running the latest
    python 2.x version, this should also work:
    #!/usr/bin/python2

    2009/11/11 Benjamin Kaplan <>:
    > On Wed, Nov 11, 2009 at 12:16 PM, kj <> wrote:
    >>
    >>
    >>
    >>
    >> I have a script that must be run with Python 2.6.x.  If one tries
    >> to run it with, say, 2.5.x, *eventually* it runs into problems and
    >> crashes.  (The failure is quicker if one attempts to run it with
    >> Python 3.x.)
    >>
    >> Is there some way to specify at the very beginning of the script
    >> the acceptable range of Python versions?
    >>

    >
    > min_version = (2,6)
    > import sys
    > if sys.version_info < min_version :
    >   print >> stderr, "must be run with at least Python 2.6"
    >   sys.exit(1)
    >
    >
    >> TIA!
    >>
    >> kynn
    >>
    >> P.S. I know that I can hardcode the path to a specific intpreter
    >> in the #! line, but I'm trying to keep the code a bit more general
    >> than that.
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>

    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Javier Collado, Nov 11, 2009
    #4
  5. On Wed, Nov 11, 2009 at 1:28 PM, Javier Collado
    <> wrote:
    > Hello,
    >
    > If you are working on linux, you can change the shebang line from:
    > #!/usr/bin/python
    >
    > to:
    > #!/usr/bin/python2.6
    >
    > Best regards,
    >    Javier
    >
    > P.S. If you just want to avoid python 3 while running the latest
    > python 2.x version, this should also work:
    > #!/usr/bin/python2
    >


    True, except that it doesn't meet the OP's requirements. The OP wanted
    to be able to specify a range of versions. The problem with changing
    the shebang line is that there's no way to say "python 2.5 or 2.6 or
    2.7"

    Regardless, it's much better to do #!/usr/bin/evn python2.6 instead of
    hard-coding the path because not everyone has their interpreter in the
    same spot. I have the Macports python 2.6.4 installed in
    /opt/local/bin and I'd get kind of annoyed if a script, insisted on
    using the 2.6.1 that came with the system, especially if it depends on
    3rd party libraries.

    > 2009/11/11 Benjamin Kaplan <>:
    >> On Wed, Nov 11, 2009 at 12:16 PM, kj <> wrote:
    >>>
    >>>
    >>>
    >>>
    >>> I have a script that must be run with Python 2.6.x.  If one tries
    >>> to run it with, say, 2.5.x, *eventually* it runs into problems and
    >>> crashes.  (The failure is quicker if one attempts to run it with
    >>> Python 3.x.)
    >>>
    >>> Is there some way to specify at the very beginning of the script
    >>> the acceptable range of Python versions?
    >>>

    >>
    >> min_version = (2,6)
    >> import sys
    >> if sys.version_info < min_version :
    >>   print >> stderr, "must be run with at least Python 2.6"
    >>   sys.exit(1)
    >>
    >>
    >>> TIA!
    >>>
    >>> kynn
    >>>
    >>> P.S. I know that I can hardcode the path to a specific intpreter
    >>> in the #! line, but I'm trying to keep the code a bit more general
    >>> than that.
    >>> --
    >>> http://mail.python.org/mailman/listinfo/python-list
    >>>

    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>

    >
     
    Benjamin Kaplan, Nov 11, 2009
    #5
  6. kj

    kj Guest

    In <> Benjamin Kaplan <> writes:

    >On Wed, Nov 11, 2009 at 12:16 PM, kj <> wrote:
    >>
    >>
    >>
    >>
    >> I have a script that must be run with Python 2.6.x. =A0If one tries
    >> to run it with, say, 2.5.x, *eventually* it runs into problems and
    >> crashes. =A0(The failure is quicker if one attempts to run it with
    >> Python 3.x.)
    >>
    >> Is there some way to specify at the very beginning of the script
    >> the acceptable range of Python versions?
    >>


    >min_version =3D (2,6)
    >import sys
    >if sys.version_info < min_version :
    > print >> stderr, "must be run with at least Python 2.6"
    > sys.exit(1)


    Thanks!

    kynn
     
    kj, Nov 11, 2009
    #6
  7. Yinon Ehrlich, Nov 12, 2009
    #7
  8. kj

    kj Guest

    kj, Nov 14, 2009
    #8
    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. Greg
    Replies:
    1
    Views:
    580
    Scott Allen
    Jan 25, 2006
  2. =?Utf-8?B?dGhlV2l6YXJkMQ==?=

    Publish and specify Major, Minor Version numbers using asp.net 2.0

    =?Utf-8?B?dGhlV2l6YXJkMQ==?=, Apr 26, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,688
    Juan T. Llibre
    Apr 26, 2006
  3. V Green
    Replies:
    0
    Views:
    858
    V Green
    Feb 5, 2008
  4. PA Bear [MS MVP]
    Replies:
    0
    Views:
    969
    PA Bear [MS MVP]
    Feb 5, 2008
  5. MowGreen [MVP]
    Replies:
    5
    Views:
    2,020
    PA Bear [MS MVP]
    Feb 9, 2008
Loading...

Share This Page