/usr/bin/env python, force a version

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • manatlan@gmail.com

    #1

    /usr/bin/env python, force a version

    I've got a trouble, and i think that anybody there can help me

    I've got a python script which i distribute in somes packages for *nix.
    This script is full of python and need python 2.4 ! And i'd like to
    display a message when the user doesn't have a python2.4 version.

    if i put, at the top of the script (which can be done with /usr/bin env
    too):

    #/usr/bin/python2.4
    it will be good, but py2.3 users can't run the script, and the message
    comes from the system ;-( ... it can't be a message from my script ;-(

    #/usr/bin/python2.3
    it will not be good, because my scrill will not work at all ;-)

    #/usr/bin/python
    it will select the defined python version of the platform ... And here
    is the problem. On debian/sid, users have python2.3 (default), et py2.4
    : So the script will start with 2.3, and my message will be displayed.
    But it could work because there is a py2.4 on the machine ;-(.

    I'd like to make my script (or a starter script)
    which will be able to detect all python versions of the machine. And
    run my script with the good one ; py2.4, or display a message to the
    user to say her it must install py2.4 ...

    I hope you understand my needs. Is there a python/bash mechanism to
    override the default python version of the system ... and run the
    script with any version of python (but the most recent) ?
    or can you explain me how to do that ? the simplest way ?

  • Laszlo Zsolt Nagy

    #2
    Re: /usr/bin/env python, force a version

    manatlan@gmail. com wrote:
    [color=blue]
    >I've got a trouble, and i think that anybody there can help me
    >
    >I've got a python script which i distribute in somes packages for *nix.
    >This script is full of python and need python 2.4 ! And i'd like to
    >display a message when the user doesn't have a python2.4 version.
    >
    >[color=green][color=darkred]
    >>> import sys
    >>> sys.version[/color][/color][/color]
    '2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    There is also

    sys.hexversion
    sys.api_version
    sys.version_inf o*

    Read this:

    *http://docs.python.org/lib/module-sys.html


    Les

    **

    Comment

    • Fredrik Lundh

      #3
      Re: /usr/bin/env python, force a version

      "manatlan@gmail .com" wrote:
      [color=blue]
      > I've got a trouble, and i think that anybody there can help me
      >
      > I've got a python script which i distribute in somes packages for *nix.
      > This script is full of python and need python 2.4 ! And i'd like to
      > display a message when the user doesn't have a python2.4 version.[/color]
      [color=blue]
      > I'd like to make my script (or a starter script)
      > which will be able to detect all python versions of the machine. And
      > run my script with the good one ; py2.4, or display a message to the
      > user to say her it must install py2.4 ...[/color]

      the approach used in bzr might work for you:

      #
      # application boot script

      import os, sys

      try:
      version_info = sys.version_inf o
      except AttributeError:
      version_info = 1, 5 # 1.5 or older

      REINVOKE = "__MYAPP_REINVO KE"
      NEED_VERS = (2, 4)
      KNOWN_PYTHONS = ('python2.4',)

      if version_info < NEED_VERS:
      if not os.environ.has_ key(REINVOKE):
      # mutating os.environ doesn't work in old Pythons
      os.putenv(REINV OKE, "1")
      for python in KNOWN_PYTHONS:
      try:
      os.execvp(pytho n, [python] + sys.argv)
      except OSError:
      pass
      print >>sys.stderr, "error: cannot find a suitable python interpreter"
      print >>sys.stderr, " (need %d.%d or later)" % NEED_VERS
      sys.exit(1)

      if hasattr(os, "unsetenv") :
      os.unsetenv(REI NVOKE)

      #
      # get things going!

      import myapp
      myapp.run()

      # end of boot script

      (the actual bzr source is shipped under the GPL, but 1) it's trivial, and 2) I contributed
      the code in the first place, so I'd say it's safe to "steal this code" ;-)

      </F>



      Comment

      • Roel Schroeven

        #4
        Re: /usr/bin/env python, force a version

        Laszlo Zsolt Nagy wrote:[color=blue]
        > manatlan@gmail. com wrote:
        >[color=green]
        >> I've got a trouble, and i think that anybody there can help me
        >>
        >> I've got a python script which i distribute in somes packages for *nix.
        >> This script is full of python and need python 2.4 ! And i'd like to
        >> display a message when the user doesn't have a python2.4 version.
        >>
        >>[color=darkred]
        >>>> import sys
        >>>> sys.version[/color][/color]
        > '2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]'[/color]

        Yes, but the problem is also that Debian (not only Sid, but also stable
        and testing) has python 2.4 but it is not the default, i.e.
        /usr/bin/python is a symlink to /usr/bin/python2.3 even if
        /usr/bin/python2.4 is available to.

        It would be nice if there was a general way to specify that a script
        should be interpreted by python2.4, even if it is not the default, in a
        way that works on all platforms. But I don't see how that should work.

        --
        If I have been able to see further, it was only because I stood
        on the shoulders of giants. -- Isaac Newton

        Roel Schroeven

        Comment

        • Laszlo Zsolt Nagy

          #5
          Re: /usr/bin/env python, force a version

          Roel Schroeven wrote:
          [color=blue]
          >Laszlo Zsolt Nagy wrote:
          >
          >[color=green]
          >>manatlan@gmai l.com wrote:
          >>
          >>
          >>[color=darkred]
          >>>I've got a trouble, and i think that anybody there can help me
          >>>
          >>>I've got a python script which i distribute in somes packages for *nix.
          >>>This script is full of python and need python 2.4 ! And i'd like to
          >>>display a message when the user doesn't have a python2.4 version.
          >>>
          >>>
          >>>
          >>>
          >>>>>import sys
          >>>>>sys.versio n
          >>>>>
          >>>>>[/color]
          >>'2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]'
          >>
          >>[/color]
          >
          >Yes, but the problem is also that Debian (not only Sid, but also stable
          >and testing) has python 2.4 but it is not the default, i.e.
          >/usr/bin/python is a symlink to /usr/bin/python2.3 even if
          >/usr/bin/python2.4 is available to.
          >
          >[/color]
          Hmm.

          Idea one:

          Create a list of possible locations. First of all, use your PATH, and
          then add common locations:

          ['/bin','/usr/bin','/opt/bin','/usr/local/bin'] # etc.

          Then use a list of the possible executable names:

          ['python','pytho n2.3','python2. 4'] # etc

          Finally using these combinations, execute each executeable with

          python -V

          and examine its output. This may work, but I'm affraid there is no
          general solution.
          The system administrator can install different python versions to
          virtually any location.
          But I don't think you should be affraid of that. If a system admin
          installs different
          versions into strange locations, then he will take the responsibility to
          fix python programs
          that need a specific version. But in most cases, looking for a python
          executable
          on your PATH should be enough; and it is the most I would expect from an
          application. :-)

          Last idea:

          - create a configuration file that resides beside your Python program
          - take the path to the good executeable there
          - if the program was started with the wrong version, but you have
          the path to the good one (from the config file), then re-execute
          - otherwise print an error message telling the required version AND
          how the user can set it up in the config file

          Third idea (for Windows only): read available versions from the
          registry. ;-)

          Best,

          Les

          Comment

          • Michael Ekstrand

            #6
            Re: /usr/bin/env python, force a version

            On Thursday 06 October 2005 06:25, manatlan@gmail. com wrote:[color=blue]
            > I hope you understand my needs. Is there a python/bash mechanism to
            > override the default python version of the system ... and run the
            > script with any version of python (but the most recent) ?
            > or can you explain me how to do that ? the simplest way ?[/color]

            This solution makes a few assumptions... but it should work in the
            majority of cases.

            The principle is that Python is, in my experience, *usually* installed
            as python2.4 or whatever - even ./configure && make && make install in
            the tarball makes python a symbolic link to a python2.4 executable.
            Assuming that this is the case, and that python2.4 will never be any
            other version of Python:

            #!/bin/sh

            APPPATH=/path/to/app
            PYTHON=`which python2.4`

            if [ $? != 0 ]; then
            echo "This program requires Python 2.4 to be installed." >/dev/stderr
            exit 1
            fi

            "$PYTHON" "$APPATH" "$@"

            Problems: Requires Python 2.4 to be installed as python2.4, and doesn't
            have upward compatibility (i.e. 2.5). But it's at least as good as
            #!/usr/bin/env python2.4, and it gives a clean error message.

            -Michael

            Comment

            • Jorgen Grahn

              #7
              Re: /usr/bin/env python, force a version

              On 6 Oct 2005 04:25:29 -0700, manatlan@gmail. com <manatlan@gmail .com> wrote:[color=blue]
              > I've got a trouble, and i think that anybody there can help me
              >
              > I've got a python script which i distribute in somes packages for *nix.
              > This script is full of python and need python 2.4 ! And i'd like to
              > display a message when the user doesn't have a python2.4 version.
              >
              > if i put, at the top of the script (which can be done with /usr/bin env
              > too):[/color]

              One data point, from the distutils manual
              (http://docs.python.org/dist/node10.html):

              Scripts are files containing Python source code, intended to be started
              from the command line. Scripts don't require Distutils to do anything very
              complicated. The only clever feature is that if the first line of the
              script starts with #! and contains the word `python'', the Distutils will
              adjust the first line to refer to the current interpreter location.

              which I interpret as "distutils will force the script to run the same Python
              as setup.py is running". Which, in turn, means that you could detect the
              absence of Python 2.4 at install time.

              /Jorgen

              --
              // Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
              \X/ algonet.se> R'lyeh wgah'nagl fhtagn!

              Comment

              Working...