Stupid question: Making scripts python-scripts

Discussion in 'Python' started by Jan Danielsson, Jul 21, 2005.

  1. Hello all,

    How do I make a python script actually a _python_ in unix:ish
    environments?

    I know about adding:
    #!/bin/sh

    ..as the first row in a shell script, but when I installed python on
    a NetBSD system, I didn't get a "python" executable; only a "python2.4"
    executable.

    Adding "#!/usr/pkg/bin/python2.4" as the first row in the script
    would probably work, but that would be too specific for the system I'm
    using, imho.

    I saw someone using "#!/usr/bin/env python", but that failed on the
    system I'm using, so I assume that's something specific too (or is the
    installation broken?).
     
    Jan Danielsson, Jul 21, 2005
    #1
    1. Advertising

  2. Jan Danielsson

    Guest

    On your system, do:
    which python2.4

    That will give you the full path to the python2.4 binary (let's call it
    "path/to/py24").

    Then add:
    #!/path/to/py24

    ....to the top of your script.

    And make sure the file is chmod'd +x
     
    , Jul 21, 2005
    #2
    1. Advertising

  3. Jan Danielsson

    Guest

    oops... I missed the "too specific comment." Sorry =)
     
    , Jul 21, 2005
    #3
  4. Jan Danielsson

    Guest

    You could also set your "python" environment variable on the system...
    set it to be "/path/to/python2.4". Then use the "#!/usr/bin/env
    python" trick. Just make sure that env is working for you, first.
     
    , Jul 21, 2005
    #4
  5. Jan Danielsson

    Bill Mill Guest

    On 7/21/05, Jan Danielsson <> wrote:
    > Hello all,
    >
    > How do I make a python script actually a _python_ in unix:ish
    > environments?
    >
    > I know about adding:
    > #!/bin/sh
    >
    > ..as the first row in a shell script, but when I installed python on
    > a NetBSD system, I didn't get a "python" executable; only a "python2.4"
    > executable.
    >
    > Adding "#!/usr/pkg/bin/python2.4" as the first row in the script
    > would probably work, but that would be too specific for the system I'm
    > using, imho.
    >
    > I saw someone using "#!/usr/bin/env python", but that failed on the
    > system I'm using, so I assume that's something specific too (or is the
    > installation broken?).


    The env program [1], which usually exists at least on a linux system,
    executes the program given as its argument. Thus, "/usr/bin/env
    python" tries to executes python, which bash will then use to run the
    python script. As long as env exists, and python is somewhere in the
    PATH, this is a fairly portable way to run python scripts.

    Does BSD really not come with the env program? I bet there's an
    equivalent you could symlink to it. Unfortunately, I've never BSDed,
    so I can't help you find it. To get a workable subset of the normal
    env functionality, you could try (assuming you use bash):

    /home/llimllib $ echo "$@" > /usr/bin/env
    /home/llimllib $ chmod a+x /usr/bin/env

    Peace
    Bill Mill
    bill.mill at gmail.com

    [1]: http://rootr.net/man/man/env/1
     
    Bill Mill, Jul 21, 2005
    #5
  6. Jan Danielsson

    Bill Mill Guest

    On 7/21/05, Bill Mill <> wrote:
    > On 7/21/05, Jan Danielsson <> wrote:
    > > Hello all,
    > >
    > > How do I make a python script actually a _python_ in unix:ish
    > > environments?
    > >
    > > I know about adding:
    > > #!/bin/sh
    > >
    > > ..as the first row in a shell script, but when I installed python on
    > > a NetBSD system, I didn't get a "python" executable; only a "python2.4"
    > > executable.
    > >
    > > Adding "#!/usr/pkg/bin/python2.4" as the first row in the script
    > > would probably work, but that would be too specific for the system I'm
    > > using, imho.
    > >
    > > I saw someone using "#!/usr/bin/env python", but that failed on the
    > > system I'm using, so I assume that's something specific too (or is the
    > > installation broken?).

    >
    > The env program [1], which usually exists at least on a linux system,
    > executes the program given as its argument. Thus, "/usr/bin/env
    > python" tries to executes python, which bash will then use to run the
    > python script. As long as env exists, and python is somewhere in the
    > PATH, this is a fairly portable way to run python scripts.
    >
    > Does BSD really not come with the env program? I bet there's an
    > equivalent you could symlink to it. Unfortunately, I've never BSDed,
    > so I can't help you find it. To get a workable subset of the normal
    > env functionality, you could try (assuming you use bash):
    >
    > /home/llimllib $ echo "$@" > /usr/bin/env
    > /home/llimllib $ chmod a+x /usr/bin/env
    >


    ahhh, that should be:

    /home/llimllib $ echo "\$@" > /usr/bin/env

    otherwise bash tries to substitute into the string. Sorry bout that.

    Peace
    Bill Mill
    bill.mill at gmail.com
     
    Bill Mill, Jul 21, 2005
    #6
  7. Jan Danielsson wrote:
    > Hello all,
    >
    > How do I make a python script actually a _python_ in unix:ish
    > environments?
    >
    > I know about adding:
    > #!/bin/sh
    >
    > ..as the first row in a shell script, but when I installed python on
    > a NetBSD system, I didn't get a "python" executable; only a "python2.4"
    > executable.
    >
    > Adding "#!/usr/pkg/bin/python2.4" as the first row in the script
    > would probably work, but that would be too specific for the system I'm
    > using, imho.


    What about:

    ln /usr/pkg/bin/python2.4 /usr/bin/python

    then
    #!/usr/bin/python

    in your script, and you should be done (dont forget to chmod +x your
    script of course)

    Or I'm I missing something specific to xxxBSD ?

    > I saw someone using "#!/usr/bin/env python", but that failed on the
    > system I'm using,


    How do you execute the python interpreter ? If you need to type
    "python2.4" (not just "python"), then obviously 'usr/bin/env python'
    syntax shouldn't work either.

    My 2 cents
    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno modulix, Jul 21, 2005
    #7
  8. Jan Danielsson wrote:

    > Hello all,
    >
    > How do I make a python script actually a _python_ in unix:ish
    > environments?
    >
    > I know about adding:
    > #!/bin/sh
    >
    > ..as the first row in a shell script, but when I installed python on
    > a NetBSD system, I didn't get a "python" executable; only a "python2.4"
    > executable.
    >
    > Adding "#!/usr/pkg/bin/python2.4" as the first row in the script
    > would probably work, but that would be too specific for the system I'm
    > using, imho.
    >
    > I saw someone using "#!/usr/bin/env python", but that failed on the
    > system I'm using, so I assume that's something specific too (or is the
    > installation broken?).


    You could
    a) create a symlink to your python executable in a well known location, e.g.
    ln -s /usr/pkg/bin/python2.4 /usr/bin/python
    And use either "#!/usr/bin/python" (which should work on most UNIXoid
    systems with python installed) or "#!/usr/bin/env python"

    b) use the absolute path /usr/pkg/bin/python2.4 for your script (in order to
    have it running on your system) and use distutils to create a setup.py
    script for distribution. IIRC setup.py will recognize the shebang of your
    scripts and replace it with the proper path to python of the target system
    during installation.

    --
    Benjamin Niemann
    Email: pink at odahoda dot de
    WWW: http://www.odahoda.de/
     
    Benjamin Niemann, Jul 21, 2005
    #8
  9. Jan Danielsson

    Mike Meyer Guest

    Bill Mill <> writes:

    > On 7/21/05, Jan Danielsson <> wrote:
    >> Hello all,
    >>
    >> How do I make a python script actually a _python_ in unix:ish
    >> environments?
    >>
    >> I know about adding:
    >> #!/bin/sh
    >>
    >> ..as the first row in a shell script, but when I installed python on
    >> a NetBSD system, I didn't get a "python" executable; only a "python2.4"
    >> executable.
    >>
    >> Adding "#!/usr/pkg/bin/python2.4" as the first row in the script
    >> would probably work, but that would be too specific for the system I'm
    >> using, imho.
    >>
    >> I saw someone using "#!/usr/bin/env python", but that failed on the
    >> system I'm using, so I assume that's something specific too (or is the
    >> installation broken?).

    >
    > The env program [1], which usually exists at least on a linux system,
    > executes the program given as its argument. Thus, "/usr/bin/env
    > python" tries to executes python, which bash will then use to run the
    > python script. As long as env exists, and python is somewhere in the
    > PATH, this is a fairly portable way to run python scripts.


    env doesn't invoke the shell, it uses execvp, which "duplicates the
    actions of the shell in searching for an executable." Further, he's on
    NetBSD - he may not have bash installed. My FreeBSD box certainly
    doesn't.

    > Does BSD really not come with the env program? I bet there's an
    > equivalent you could symlink to it. Unfortunately, I've never BSDed,
    > so I can't help you find it. To get a workable subset of the normal
    > env functionality, you could try (assuming you use bash):


    NetBSD comes with an env. His env is failing because he doesn't have a
    "python" command installed.

    This appears to be wart(?) in the NetBSD packaging system. To allow
    multiple versions of FreeBSD to coexist, it doesn't install a "python"
    command at all, but instead leaves the versioned one around.

    Two solutions: have your scripts use #!/usr/pkg/bin/python2.4. That
    way, when you install a new python, they will keep using the old
    one. That will insure they won't break when you upgrade.

    However, such breakage is pretty rare in practice. I'd pick a favorite
    bin directory and symlink from python there to /usr/pkg/bin/python2.4,
    then use "#!/usr/bin/env python" in your scripts.

    While I'm on the topic, I think I'll share my favorite cool trick.

    #!/usr/opt/bin/mypythonscript

    Doesn't work because Unix won't let you use an interpreted script as
    the interpreter (is this true for all variants?). However,

    #!/usr/bin/env mypythonscript

    works like a charm.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Jul 22, 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. Replies:
    5
    Views:
    1,919
    Andrew Thompson
    Jan 12, 2005
  2. Jp Calderone
    Replies:
    0
    Views:
    474
    Jp Calderone
    Jul 21, 2005
  3. Brandon McCombs
    Replies:
    4
    Views:
    529
    Richard Wheeldon
    Aug 28, 2006
  4. davidj411
    Replies:
    0
    Views:
    563
    davidj411
    Jun 27, 2008
  5. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,049
Loading...

Share This Page