By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Python executable

P: n/a
Hello,

I'm running Ubuntu Linux 5.04.

I just started teaching myself Python today, and have been reading a
few things to get started. I came across something in one (namely
http://docs.python.org/tut/node4.htm...00000000000000)
that confused me a little.

It says:

------------

On BSD'ish Unix systems, Python scripts can be made directly
executable, like shell scripts, by putting the line

#! /usr/bin/env python

(assuming that the interpreter is on the user's PATH) at the beginning
of the script and giving the file an executable mode. The "#!" must be
the first two characters of the file. On some platforms, this first
line must end with a Unix-style line ending ("\n"), not a Mac OS ("\r")
or Windows ("\r\n") line ending. Note that the hash, or pound,
character, "#", is used to start a comment in Python.

The script can be given a executable mode, or permission, using the
chmod command:

$ chmod +x myscript.py

-----------

So I created a file named helloworld.py, and put in it:

#! /usr/bin/env python
print "Hello, world!"

and then used
$ chmod +x helloworld.py
to set the permissions. Finally, I went to my terminal and typed
$ helloworld.py
but I was given the bash: helloworld.py: command not found error.

Can someone tell me
(1)Am I right in saying that something is directly executable if I can
enter the filename in the command line and it runs?
(2)Am I setting up the script to be directly executable correctly?
and (3)Am I trying to run the directly executable script correctly?

Thanks a lot. I hope this post isn't too hard to follow; I know I'm
asking a lot.

~~Ted Present

Sep 6 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
presentt wrote:
Hello,

I'm running Ubuntu Linux 5.04.

I just started teaching myself Python today, and have been reading a
few things to get started. I came across something in one (namely
http://docs.python.org/tut/node4.htm...00000000000000)
that confused me a little.

It says:

------------

On BSD'ish Unix systems, Python scripts can be made directly
executable, like shell scripts, by putting the line

#! /usr/bin/env python

(assuming that the interpreter is on the user's PATH) at the beginning
of the script and giving the file an executable mode. The "#!" must be
the first two characters of the file. On some platforms, this first
line must end with a Unix-style line ending ("\n"), not a Mac OS ("\r")
or Windows ("\r\n") line ending. Note that the hash, or pound,
character, "#", is used to start a comment in Python.

The script can be given a executable mode, or permission, using the
chmod command:

$ chmod +x myscript.py

-----------

So I created a file named helloworld.py, and put in it:

#! /usr/bin/env python
print "Hello, world!"

and then used
$ chmod +x helloworld.py
to set the permissions. Finally, I went to my terminal and typed
$ helloworld.py
but I was given the bash: helloworld.py: command not found error.

Can someone tell me
(1)Am I right in saying that something is directly executable if I can
enter the filename in the command line and it runs?
Yes
(2)Am I setting up the script to be directly executable correctly?
No. There should be no embedded space after the "#!" that indicates the
first line identifies the program interpreter. The first line should read

#!/usr/bin/env python
and (3)Am I trying to run the directly executable script correctly?
Only if the current directory (".") is on your path (which you can see
with the command

echo $PATH

in your command shell).
Thanks a lot. I hope this post isn't too hard to follow; I know I'm
asking a lot.

It's clear you're new to comp.lang.python - this is a perfectly
acceptable request. Hope this answer helps.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 6 '05 #2

P: n/a
"presentt" <pr******@gmail.com> said:
Hello,

I'm running Ubuntu Linux 5.04.

I just started teaching myself Python today, and have been reading a
few things to get started. I came across something in one (namely
http://docs.python.org/tut/node4.htm...00000000000000)
that confused me a little.

It says:

------------

On BSD'ish Unix systems, Python scripts can be made directly
executable, like shell scripts, by putting the line

#! /usr/bin/env python

(assuming that the interpreter is on the user's PATH) at the beginning
of the script and giving the file an executable mode. The "#!" must be
the first two characters of the file. On some platforms, this first
line must end with a Unix-style line ending ("\n"), not a Mac OS ("\r")
or Windows ("\r\n") line ending. Note that the hash, or pound,
character, "#", is used to start a comment in Python.

The script can be given a executable mode, or permission, using the
chmod command:

$ chmod +x myscript.py

-----------

So I created a file named helloworld.py, and put in it:

#! /usr/bin/env python
print "Hello, world!"

and then used
$ chmod +x helloworld.py
to set the permissions. Finally, I went to my terminal and typed
$ helloworld.py
but I was given the bash: helloworld.py: command not found error.

Can someone tell me
(1)Am I right in saying that something is directly executable if I can
enter the filename in the command line and it runs?
(2)Am I setting up the script to be directly executable correctly?
and (3)Am I trying to run the directly executable script correctly?

Thanks a lot. I hope this post isn't too hard to follow; I know I'm
asking a lot.

~~Ted Present


[3] cd to the directory that contains helloworld.py, then
./helloworld.py
(When you enter helloworld.py as a command, the shell
searches the PATH for helloworld.py and if it is not
found, it complains. If you tell it that it's in the
current directory, it will get it without searching.)

Sep 6 '05 #3

P: n/a
On Monday 05 September 2005 21:54, presentt <pr******@gmail.com>
(<11*********************@g43g2000cwa.googlegroups .com>) wrote:
So I created a file named helloworld.py, and put in it:

#! /usr/bin/env python
print "Hello, world!"

and then used
$ chmod +x helloworld.py
to set the permissions. Finally, I went to my terminal and typed
$ helloworld.py
but I was given the bash: helloworld.py: command not found error.

Can someone tell me
(1)Am I right in saying that something is directly executable if I can
enter the filename in the command line and it runs?
Almost. For more information, you might read about the PATH variable.
(2)Am I setting up the script to be directly executable correctly?
Yes.
and (3)Am I trying to run the directly executable script correctly?


No. The best (safest) method is to type

./helloworld.py

to tell your shell that the script is in the current directory.
Sep 6 '05 #4

P: n/a
Thanks everyone! I think I'm going to stick with Python; at least I
know there are some people here willing to help.

Sep 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.