473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Stupid question: Making scripts python-scripts

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?).
Jul 21 '05 #1
8 1995
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 '05 #2
oops... I missed the "too specific comment." Sorry =)

Jul 21 '05 #3
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 '05 #4
On 7/21/05, Bill Mill <bi*******@gmail.com> wrote:
On 7/21/05, Jan Danielsson <ja************@gmail.com> 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
Jul 21 '05 #5
On 7/21/05, Jan Danielsson <ja************@gmail.com> 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
Jul 21 '05 #6
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 'o****@xiludom.gro'.split('@')])"
Jul 21 '05 #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.

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/
Jul 21 '05 #8
Bill Mill <bi*******@gmail.com> writes:
On 7/21/05, Jan Danielsson <ja************@gmail.com> 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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: UJ | last post by:
What's the easiest way (without setting up a style sheet) to set the font for an area of text. I need to be able to specify the exact font and font size - not the 1-7 numbers IE uses. I want to be...
65
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the...
15
by: sparks | last post by:
We get more and more data done in excel and then they want it imported into access. The data is just stupid....values of 1 to 5 we get a lot of 0's ok that alright but 1-jan ? we get colums...
4
by: K8CPA | last post by:
Hi Guys, I've got a question from a stupid question from a clueless computer operator's standpoint. I use Windows XP. SP2, and I've got all these neat little programs. What language do they...
1
by: M.N.A.Smadi | last post by:
hi guys; sorry for sending a perl question here, but python guy "HAD TO" look at perl code; how can i split a string that contains white spaces and '_' any clue? thanks
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
19
by: Leslie Kis-Adam | last post by:
Hi everyone! Can anyone explain me this program? I'm getting really confused. thx in advance. Laszlo Kis-Adam <dfighter@freemail.hu> "
1
by: Stef Mientki | last post by:
Does anyone know the equivalent of the MatLab "diff" function. The "diff" functions calculates the difference between 2 succeeding elements of an array. I need to detect (fast) the falling edge of...
0
by: hide1713 | last post by:
Hi everyone We are making a disk less workstation environment which support python.We use PXE DHCP TFTP to boot up disk less system.There are three choices as i know to set up a python environment...
1
by: catalinfest | last post by:
I see on this link "# Python is Stupid" http://schlake.livejournal.com/809567.html Code is #!/usr/bin/env python for flour in range( 50, 1000 + 1, 5): # Python is Stupid print '%.2f %.2f %.2f'...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.