473,499 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python executable

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
4 2449
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4031
by: Thomas Aanensen | last post by:
Python could be located at different paths on computers (e.g. c:\python). How can I find this path? Thomas
47
3606
by: Michael Scarlett | last post by:
There is an amazing article by paul graham about python, and an even better discussion about it on slashdot. The reason I point this out, is the more I read both articles, the more I realised how...
4
2087
by: mrmakent | last post by:
3 quick questions for the newsgroup: 1. Does anyone know why McMillan Installer 5b5 does not work with Python 2.4 under Linux (works with Python 2.3 just fine), and how to fix it? 2. Will...
7
3194
by: Edward Diener | last post by:
I can install Python 2.4 on the Fedora 3 Linux system, but after I do a number of Linux utilities and commands, like yum, stop working because they were dependent on the Python 2.3 installation....
37
2425
by: John Salerno | last post by:
I contacted my domain host about how Python is implemented on their server, and got this response: ------------------- Hello John, Please be informed that the implementation of python in our...
34
3920
by: Ben Sizer | last post by:
I've installed several different versions of Python across several different versions of MS Windows, and not a single time was the Python directory or the Scripts subdirectory added to the PATH...
3
3932
by: Jeffrey Froman | last post by:
Hello All, I have two python versions installed, one in /usr/bin, and one in /usr/local/bin. However, when invoking python without a full path, I get the wrong executable with the right...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.