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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rh0dium

    #1

    #!/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=loggin g.INFO

    # Set's up a basic logger
    logging.basicCo nfig(level=LOGL EVEL, format="%(ascti me)s %(name)s %
    (levelname)-8s %(message)s",
    datefmt='%d %b %Y %H:%M:%S', stream=sys.stde rr)

    try:
    module= os.path.basenam e(traceback.ext ract_stack(limi t=2)[1]
    [0]).split(".py")[0]+"."
    except:
    module = os.path.basenam e(traceback.ext ract_stack(limi t=2)[0]
    [0]).split(".py")[0]+"."

    def main(loglevel=F alse):
    """ """
    # Setup Logging
    log = logging.getLogg er(module+sys._ getframe().f_co de.co_name )
    if loglevel is not False: log.setLevel(lo glevel)
    else:log.setLev el(logging.WARN )

    log.debug("I LOVE LOGGING")

    if __name__ == '__main__':
    sys.exit(main(l oglevel=LOGLEVE L))


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

  • Steve Holden

    #2
    Re: #!/usr/bin/env python &gt; 2.4?

    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

    Comment

    • rh0dium

      #3
      Re: #!/usr/bin/env python &gt; 2.4?

      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



      Comment

      • Stargaming

        #4
        Re: #!/usr/bin/env python &gt; 2.4?

        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("Y ou 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

        Comment

        • rh0dium

          #5
          Re: #!/usr/bin/env python &gt; 2.4?

          On Mar 20, 12:30 pm, Stargaming <stargam...@gma il.comwrote:
          >
          from sys import version_info
          if version_info[0] < 2 or version_info[1] < 4:
          raise RuntimeError("Y ou 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.

          Comment

          • Jon Ribbens

            #6
            Re: #!/usr/bin/env python &gt; 2.4?

            In article <etpcon$1g8r$1@ ulysses.news.ti scali.de>, Stargaming wrote:
            from sys import version_info
            if version_info[0] < 2 or version_info[1] < 4:
            raise RuntimeError("Y ou 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 ...

            Comment

            • Sion Arrowsmith

              #7
              Re: #!/usr/bin/env python &gt; 2.4?

              Jon Ribbens <jon+usenet@une quivocal.co.ukw rote:
              >You want:
              >
              if sys.hexversion < 0x020400f0:
              ... error ...

              "Readabilit y counts."

              if sys.version_inf o < (2, 4):
              ... error ...

              --
              \S -- siona@chiark.gr eenend.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

              Comment

              • Jon Ribbens

                #8
                Re: #!/usr/bin/env python &gt; 2.4?

                In article <4Rf*J7eGr@news .chiark.greenen d.org.uk>, Sion Arrowsmith wrote:
                Jon Ribbens <jon+usenet@une quivocal.co.ukw rote:
                > if sys.hexversion < 0x020400f0:
                > ... error ...
                >
                "Readabilit y counts."
                >
                if sys.version_inf o < (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.

                Comment

                • starGaming@gmail.com

                  #9
                  Re: #!/usr/bin/env python &gt; 2.4?

                  On Mar 21, 11:07 am, Jon Ribbens <jon+use...@une quivocal.co.ukw rote:
                  In article <etpcon$1g8...@ ulysses.news.ti scali.de>, Stargaming wrote:
                  from sys import version_info
                  if version_info[0] < 2 or version_info[1] < 4:
                  raise RuntimeError("Y ou 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'.

                  Comment

                  • Sander Steffann

                    #10
                    Re: #!/usr/bin/env python &gt; 2.4?

                    Hi,

                    Op 21-mrt-2007, om 20:41 heeft starGaming@gmai l.com het volgende
                    geschreven:
                    On Mar 21, 11:07 am, Jon Ribbens <jon+use...@une quivocal.co.ukw rote:
                    >In article <etpcon$1g8...@ ulysses.news.ti scali.de>, Stargaming wrote:
                    >>from sys import version_info
                    >>if version_info[0] < 2 or version_info[1] < 4:
                    >> raise RuntimeError("Y ou 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

                    Comment

                    • starGaming@gmail.com

                      #11
                      Re: #!/usr/bin/env python &gt; 2.4?

                      On Mar 21, 11:11 pm, Sander Steffann <s.steff...@com putel.nlwrote:
                      Hi,
                      >
                      Op 21-mrt-2007, om 20:41 heeft starGam...@gmai l.com het volgende
                      geschreven:
                      >
                      >
                      >
                      On Mar 21, 11:07 am, Jon Ribbens <jon+use...@une quivocal.co.ukw rote:
                      In article <etpcon$1g8...@ ulysses.news.ti scali.de>, Stargaming wrote:
                      >from sys import version_info
                      >if version_info[0] < 2 or version_info[1] < 4:
                      > raise RuntimeError("Y ou 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?

                      Comment

                      • Jon Ribbens

                        #12
                        Re: #!/usr/bin/env python &gt; 2.4?

                        In article <1174580529.081 658.326800@b75g 2000hsg.googleg roups.com>, starGaming@gmai l.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?

                        Comment

                        • starGaming@gmail.com

                          #13
                          Re: #!/usr/bin/env python &gt; 2.4?

                          On Mar 22, 5:23 pm, Jon Ribbens <jon+use...@une quivocal.co.ukw rote:
                          In article <1174580529.081 658.326...@b75g 2000hsg.googleg roups.com>, starGam...@gmai l.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.

                          Comment

                          • Gabriel Genellina

                            #14
                            Re: #!/usr/bin/env python &gt; 2.4?

                            En Wed, 21 Mar 2007 07:07:20 -0300, Jon Ribbens
                            <jon+usenet@une quivocal.co.uke scribió:
                            In article <etpcon$1g8r$1@ ulysses.news.ti scali.de>, Stargaming wrote:
                            >from sys import version_info
                            >if version_info[0] < 2 or version_info[1] < 4:
                            > raise RuntimeError("Y ou 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_inf o < (2,4):
                            raise RuntimeError("Y ou need at least python2.4...")

                            (Does the f0 account for the prereleases?)
                            --
                            Gabriel Genellina

                            Comment

                            • Gabriel Genellina

                              #15
                              Re: #!/usr/bin/env python &gt; 2.4?

                              En Wed, 21 Mar 2007 14:42:53 -0300, Jon Ribbens
                              <jon+usenet@une quivocal.co.uke scribió:
                              In article <4Rf*J7eGr@news .chiark.greenen d.org.uk>, Sion Arrowsmith
                              wrote:
                              >Jon Ribbens <jon+usenet@une quivocal.co.ukw rote:
                              >> if sys.hexversion < 0x020400f0:
                              >> ... error ...
                              >>
                              >"Readabilit y counts."
                              >>
                              >if sys.version_inf o < (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

                              Comment

                              Working...