From: Phil H on
Hi,
Trying my hand with Python but have had a small hiccup.
Reading 'A byte of Python' and created helloworld.py as directed.

#!/usr/bin/python

# filename : helloworld.py

print 'Hello World'

At the terminal prompt cd to the file location and run from the prompt.

phil(a)grumpy:~/projects/python$ python helloworld.py
Hello World

All fine.

Then I tried the following as described in the tutorial and get the
following error

phil(a)grumpy:~/projects/python$ chmod a+x helloworld.py
phil(a)grumpy:~/projects/python$ ./helloworld.py
bash: ./helloworld.py: /usr/bin/python^M: bad interpreter: No such file
or directory

The permissions are: rwxr-xr-x.

Any help appreciated
Phil

From: Peter Otten on
Phil H wrote:

> Hi,
> Trying my hand with Python but have had a small hiccup.
> Reading 'A byte of Python' and created helloworld.py as directed.
>
> #!/usr/bin/python
> # filename : helloworld.py
> print 'Hello World'
>
> At the terminal prompt cd to the file location and run from the prompt.
>
> phil(a)grumpy:~/projects/python$ python helloworld.py
> Hello World
>
> All fine.
>
> Then I tried the following as described in the tutorial and get the
> following error
>
> phil(a)grumpy:~/projects/python$ chmod a+x helloworld.py
> phil(a)grumpy:~/projects/python$ ./helloworld.py
> bash: ./helloworld.py: /usr/bin/python^M: bad interpreter: No such file
> or directory
>
> The permissions are: rwxr-xr-x.
>
> Any help appreciated
> Phil

Did you write your script on a windows machine? Your line endings seem to be
\r\n but you need \n. You can use dos2unix to fix the line endings:


$ cat -v tmp.py
#!/usr/bin/python^M
print 'hello world'^M
$ ./tmp.py
bash: ./tmp.py: /usr/bin/python^M: bad interpreter: No such file or
directory
$ dos2unix tmp.py
$ cat -v tmp.py
#!/usr/bin/python
print 'hello world'
$ ./tmp.py
hello world
$

Peter
From: Chris Rebert on
On Sat, Jun 12, 2010 at 2:03 AM, Phil H <skilphil(a)gmail.co.za> wrote:
> Hi,
> Trying my hand with Python but have had a small hiccup.
> Reading  'A byte of Python' and created helloworld.py as directed.
>
> #!/usr/bin/python
> # filename : helloworld.py
> print 'Hello World'
>
> At the terminal prompt cd to the file location and run from the prompt.
>
> phil(a)grumpy:~/projects/python$ python helloworld.py
> Hello World
>
> All fine.
>
> Then I tried the following as described in the tutorial and get the
> following error
>
> phil(a)grumpy:~/projects/python$ chmod a+x helloworld.py
> phil(a)grumpy:~/projects/python$ ./helloworld.py
> bash: ./helloworld.py: /usr/bin/python^M: bad interpreter: No such file
> or directory
>
> The permissions are: rwxr-xr-x.
>
> Any help appreciated

Seems your file is using Windows line endings (CR+LF) rather than Unix
line endings (just LF), which is messing up the processing of the
shebang line.
Run your file thru `dos2unix`
(http://gd.tuwien.ac.at/linuxcommand.org/man_pages/dos2unix1.html ).
Further info: http://en.wikipedia.org/wiki/Newline

Also, a more generic shebang line is usually recommended:
#!/usr/bin/env python

Cheers,
Chris
--
http://blog.rebertia.com
From: Phil H on
On Sat, 12 Jun 2010 09:03:43 +0000, Phil H wrote:

> Hi,
> Trying my hand with Python but have had a small hiccup. Reading 'A byte
> of Python' and created helloworld.py as directed.
><snip>
> Any help appreciated
> Phil

Thanks Peter & Chris for your prompt replies.
The line ending was the problem.
The script was written using Gedit on Ubuntu.
Cannot find a setting in Gedit to set the line ending but it must be
there somewhere so will keep looking.
Also how do you see or check the line endings of a file?

Thanks again
Phil
From: Peter Otten on
Phil H wrote:

> On Sat, 12 Jun 2010 09:03:43 +0000, Phil H wrote:
>
>> Hi,
>> Trying my hand with Python but have had a small hiccup. Reading 'A byte
>> of Python' and created helloworld.py as directed.
>><snip>
>> Any help appreciated
>> Phil
>
> Thanks Peter & Chris for your prompt replies.
> The line ending was the problem.
> The script was written using Gedit on Ubuntu.

Strange. Did you perhaps start with a file that you got from elsewhere and
modified that? Gedit may have left the CRs untouched then.

> Cannot find a setting in Gedit to set the line ending but it must be
> there somewhere so will keep looking.
> Also how do you see or check the line endings of a file?

cat -v filename

is one option. CR (or "\r", or chr(13), or carriage return) shows up as ^M.
The ^ means "subtract 64 from the byte value", e. g.

^M = chr(ord("M")-64) = chr(77-64) = chr(13)

Peter