473,322 Members | 1,523 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,322 software developers and data experts.

why use #!/usr/bin/env python rather than #!python?

Many Python scripts I see start with the shebang line

#!/usr/bin/env python

What is the difference from using just

#!python

Regards,
Adriano.
Dec 2 '05 #1
9 7762
Adriano Ferreira wrote:
Many Python scripts I see start with the shebang line

#!/usr/bin/env python

What is the difference from using just

#!python


#v+

$ ls -l /tmp/hello.py
-rwxr-xr-x 1 klaus klaus 38 2005-12-02 14:59 /tmp/hello.py
$ cat /tmp/hello.py
#! python
print 'Hello, world!'
# eof
$ /tmp/hello.py
bash: /tmp/hello.py: python: bad interpreter: No such file or directory
$

#v-

Cheers,

--
Klaus Alexander Seistrup
Copenhagen, Denmark
http://streetkids.dk/
Dec 2 '05 #2
On 12/2/05, Klaus Alexander Seistrup <kl***@seistrup.dk> wrote:
#v+

$ ls -l /tmp/hello.py
-rwxr-xr-x 1 klaus klaus 38 2005-12-02 14:59 /tmp/hello.py
$ cat /tmp/hello.py
#! python
print 'Hello, world!'
# eof
$ /tmp/hello.py
bash: /tmp/hello.py: python: bad interpreter: No such file or directory
$

#v-


Hey, that's not fair. In your illustration above, does 'python' can be
found in the PATH? That is,

$ python /tmp/hello.py

works? If it does, probably

#!/usr/bin/python
#!/usr/bin/env python
#!python

would also work if
(1) 'python' is at '/usr/bin/python' (but that's inflexible)
(2) 'python' can be found in the environment variable path (if 'env'
is at '/usr/bin/env')
(3) 'python' can be found in the environment variable path (no need
for 'env' utility)

Cheers,
Adriano.
Dec 2 '05 #3
Adriano Ferreira skrev:
#v+

$ ls -l /tmp/hello.py
-rwxr-xr-x 1 klaus klaus 38 2005-12-02 14:59 /tmp/hello.py
$ cat /tmp/hello.py
#! python
print 'Hello, world!'
# eof
$ /tmp/hello.py
bash: /tmp/hello.py: python: bad interpreter: No such file or directory
$

#v-


Hey, that's not fair. In your illustration above, does 'python' can be
found in the PATH? That is,

$ python /tmp/hello.py

works? If it does, probably

#!/usr/bin/python
#!/usr/bin/env python
#!python

would also work if
(1) 'python' is at '/usr/bin/python' (but that's inflexible)
(2) 'python' can be found in the environment variable path (if 'env'
is at '/usr/bin/env')
(3) 'python' can be found in the environment variable path (no need
for 'env' utility)


Sure, I wasn't fair. But look here:

#v+

$ python /tmp/hello.py
Hello, world!
$ which python
/usr/bin/python
$

#v-

I do not know the syntax of the shebang-line, but perhaps an absolute
path is required?

PS: The "Mail-Copies-To: nobody" header line means that I do not wish
to receive mail replies - please follow-up to group only.

Cheers,

--
Klaus Alexander Seistrup
Copenhagen, Denmark
http://streetkids.dk/
Dec 2 '05 #4
On Fri, 2005-12-02 at 09:12, Adriano Ferreira wrote:
On 12/2/05, Klaus Alexander Seistrup <kl***@seistrup.dk> wrote:
#v+

$ ls -l /tmp/hello.py
-rwxr-xr-x 1 klaus klaus 38 2005-12-02 14:59 /tmp/hello.py
$ cat /tmp/hello.py
#! python
print 'Hello, world!'
# eof
$ /tmp/hello.py
bash: /tmp/hello.py: python: bad interpreter: No such file or directory
$

#v-


Hey, that's not fair. In your illustration above, does 'python' can be
found in the PATH? That is,

$ python /tmp/hello.py

works? If it does, probably

#!/usr/bin/python
#!/usr/bin/env python
#!python

would also work if
(1) 'python' is at '/usr/bin/python' (but that's inflexible)
(2) 'python' can be found in the environment variable path (if 'env'
is at '/usr/bin/env')
(3) 'python' can be found in the environment variable path (no need
for 'env' utility)


(3) assumes that whatever shell the user is running looks up the shebang
executable in the path, which bash, just to name one example, does not
do.

(2) and (1) require that you know where env and python live,
respectively, that's true, but env is likely to be found in an
OS-dependent standard location than python, so (2) is preferable.

HTH,

Carsten.
Dec 2 '05 #5
On 12/2/05, Carsten Haese <ca*****@uniqsys.com> wrote:
(3) assumes that whatever shell the user is running looks up the shebang
executable in the path, which bash, just to name one example, does not
do.
I think that was the answer I was looking for. So that "#!/usr/bin/env
python" is more portable than "#! python" and that's probably why it
worked for me with cygwin/bash but not for Klaus on whatever platform
he used.
(2) and (1) require that you know where env and python live,
respectively, that's true, but env is likely to be found in an
OS-dependent standard location than python, so (2) is preferable.


I agree. Only a very strange Unix-like installation would not have
'env' at '/usr/bin/env'.

Many thanks to Klaus and Carsten for helping me find out why this
convention is helpful/useful.

Best regards,
Adriano
Dec 2 '05 #6
Adriano Ferreira wrote:
So that "#!/usr/bin/env python" is more portable than "#! python"
and that's probably why it worked for me with cygwin/bash but not
for Klaus on whatever platform he used.
/me is using bash on linux.
I agree. Only a very strange Unix-like installation would not have
'env' at '/usr/bin/env'.


According to <http://www.in-ulm.de/~mascheck/various/shebang/>:

»However, env(1) is not /usr/bin/env but /bin/env at least on
Unicos 9.0.2, OpenServer 5.0.6 and IIRC on at least one older
Linux distribution. Free-, Net- and OpenBSD in turn only come
with /usr/bin/env. So the env-mechanism is increasing conven-
ience, but not strictly assuring portability.«

Cheers,

--
Klaus Alexander Seistrup
PNX · http://pnx.dk/
Dec 2 '05 #7
On 12/2/05, Klaus Alexander Seistrup <kl***@seistrup.dk> wrote:
/me is using bash on linux.
I think that was not a bash issue in my case, but a Cygwin/Win32
issue. Windows has some monstruous oddities in order to assure broken
behavior of yesterday is here today in the name of compatibility.
Examples of these everlasting broken behaviors in Windows are looking
at current directory and path no matter what you say. Cygwin isn't
immune to this.
According to <http://www.in-ulm.de/~mascheck/various/shebang/>:

»However, env(1) is not /usr/bin/env but /bin/env at least on
Unicos 9.0.2, OpenServer 5.0.6 and IIRC on at least one older
Linux distribution. Free-, Net- and OpenBSD in turn only come
with /usr/bin/env. So the env-mechanism is increasing conven-
ience, but not strictly assuring portability.«


That is to the point. Thanks.

Adriano.
Dec 2 '05 #8
In article <ma***************************************@python. org>,
Adriano Ferreira <a.**********@gmail.com> wrote:
Hey, that's not fair. In your illustration above, does 'python' can be
found in the PATH? That is,

$ python /tmp/hello.py

works? If it does, probably

#!/usr/bin/python
#!/usr/bin/env python
#!python

would also work if
(1) 'python' is at '/usr/bin/python' (but that's inflexible)
(2) 'python' can be found in the environment variable path (if 'env'
is at '/usr/bin/env')
(3) 'python' can be found in the environment variable path (no need
for 'env' utility)


Contrary to popular belief, #! is not intended for the shell,
but rather for the execve(2) system call of the UNIX operating
system. These two characters form the 16 bit "magic number"
of interpreter files. Any executable file must start with a
16 bit field that identifies it so the operating system will
know how to execute it.

In the case of a #! interpreter file, the operating system
expects the rest of that line to be the path to the file.
PATH is not searched, and is irrelevant. The only way
#!python can work, is if it's in the current working directory.

Just to help make it confusing, when this mechanism fails
and execve(2) returns an error, most shells will go on to
try to execute the file themselves, regardless of whether
there's a #! or not. csh (the shell language that doesn't
look anything like C, Bill Joy's attempt at language design
before he started over with Java) does that only if the first
line is "#"; otherwise it invokes the Bourne shell.

Donn Cave, do**@u.washington.edu
Dec 2 '05 #9
Carsten Haese <ca*****@uniqsys.com> writes:
(3) assumes that whatever shell the user is running looks up the shebang
executable in the path, which bash, just to name one example, does not
do.


For the record, on a modern Unix system, #! isn't handled by the
shell; it's handled by the kernel. #! is a "magic number" denoting a
type of executable. Some shells used to do that, and may still do that
if the underlying system doesn't handle it.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 3 '05 #10

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

Similar topics

0
by: Achim Domma | last post by:
Hello, I need mysql-python for python 2.3 on windows. I downloaded the source and tried to build it myself, but I get linker errors: mysqlclient.lib(password.obj) : error LNK2001: unresolved...
23
by: Robey Holderith | last post by:
Anyone know a good way to embed python within python? Now before you tell me that's silly, let me explain what I'd like to do. I'd like to allow user-defined scriptable objects. I'd like to...
8
by: Maurice LING | last post by:
Hi, anyone had any experiences in embedding python in python? I've tried to do this but it doesn't work. eval("from Tkinter import *") Thanks maurice
1
by: Kenneth McDonald | last post by:
Can anyone recommend something which will do one of the following: 1) Parse a Python file into an "object-oriented" syntax tree of some sort (eg. with function definitions represented by a...
9
by: Harald Massa | last post by:
Hello, I have a group of Python programms which I want to teach to "talk to each other". All run on Windows, on the same computer or in the same intranet. Security of communication is not an...
10
by: Michael Hoffman | last post by:
Does anyone have a script to convert more conventional USENET quoting style like this: John wrote: > Jacob Jingleheimer Schmidt <jjs@example.net> wrote in message-id <gratuitous-detail> on 29...
0
by: Aahz | last post by:
On Tue, Oct 07, 2008, "Martin v. L?wis" wrote: Just to emphasize this, "changes" means "bugfixes". (I'm mentioning this mainly because of the people who joined for 2.6/3.0.) For more info, see...
0
by: Steve Holden | last post by:
Terry Reedy wrote: True only if you replace "run" with "import". It also creates a new .pyc file if an existing valid .pyc was created by some other version of Python. However, I suspect the...
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...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.