First program

Discussion in 'Python' started by Phil H, Jun 12, 2010.

  1. Phil H

    Phil H Guest

    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@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@grumpy:~/projects/python$ chmod a+x helloworld.py
    phil@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
    Phil H, Jun 12, 2010
    #1
    1. Advertising

  2. Phil H

    Peter Otten Guest

    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@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@grumpy:~/projects/python$ chmod a+x helloworld.py
    > phil@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
    Peter Otten, Jun 12, 2010
    #2
    1. Advertising

  3. Phil H

    Chris Rebert Guest

    On Sat, Jun 12, 2010 at 2:03 AM, 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@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@grumpy:~/projects/python$ chmod a+x helloworld.py
    > phil@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
    Chris Rebert, Jun 12, 2010
    #3
  4. Phil H

    Phil H Guest

    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
    Phil H, Jun 12, 2010
    #4
  5. Phil H

    Peter Otten Guest

    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
    Peter Otten, Jun 12, 2010
    #5
  6. Phil H

    Phil Hansen Guest

    On Sat, 12 Jun 2010 12:51:18 +0200, Peter Otten wrote:

    > Phil H wrote:


    >> 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.


    Found it. When using 'save as' there is an option box at the bottom where
    you can select the line ending. Simple when you know how :)

    >> 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


    Thanks for the help
    Cheers
    Phil
    Phil Hansen, Jun 12, 2010
    #6
  7. Phil H

    Alister Guest

    On Sat, 12 Jun 2010 10:04:02 +0000, 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. 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


    You may want to try installing geany from your package manager, it is a
    pretty good but lightweight editor for programming with syntax
    highlighting & the option to replace tabs/spaces as req
    it also handles html,php, ruby & a whole host of others.

    before I found geany I also used bluefish which is probably more
    sophisticated but not as lightweight.



    --
    The Movement Formerly Known As Open Source

    The battle over the Open Source trademark is heating up. Software in the
    Public Interest and the Open Source Initiative both hold competing claims
    to
    the trademark. In order to put an end to the infighting, a group of free
    software advocates have founded the Association for the Movement Formerly
    Known as Open Source (AMFKOS)

    One AMFKOS founder said, "I find it ironic that a trademark representing
    free
    software is itself proprietary. This situation must change. We propose
    that
    the free software movement adopt another name besides 'Open Source'.
    Hopefully then we can all Get-Back-To-Coding(tm) instead of fighting over
    Bruce Perens' and Eric Raymond's egos."

    Rumor has it that Richard Stallman plans to mount a campaign to
    promote the phrase "GNU/Free Software" in place of "Open Source".
    In addition, the terms "Ajar Source", "Unlocked Source", "Nude Source",
    "Unclosed Source", and "Just-Type-make Software" have all
    been proposed by various Usenet or Slashdot posters.
    Alister, Jun 12, 2010
    #7
  8. En Sat, 12 Jun 2010 06:03:43 -0300, Phil H <> escribió:

    > 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@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@grumpy:~/projects/python$ chmod a+x helloworld.py
    > phil@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.


    Looks like you created helloworld.py on Windows, or using Windows-oriented
    tools (perhaps a samba drive? ftp from a Windows disk?)
    Windows text files end each line with the \r\n sequence (CR LF, bytes 0x0D
    0x0A, ^M^J). Unix (and Linux) uses only a \n (LF, 0x0A). The \r will be
    read as part of the previous line then.

    There are tools to convert back and forth those formats (dos2unix and
    unix2dos, or the crlf.py demo script in the Python source distribution).
    But to avoid problems, it's better to use the right tools for the OS
    you're working with (that is, don't use notepad to edit Linux files...)

    --
    Gabriel Genellina
    Gabriel Genellina, Jun 15, 2010
    #8
  9. Phil H

    Phil Hansen Guest

    On Tue, 15 Jun 2010 04:53:52 -0300, Gabriel Genellina wrote:
    > Looks like you created helloworld.py on Windows, or using
    > Windows-oriented tools (perhaps a samba drive? ftp from a Windows disk?)
    > Windows text files end each line with the \r\n sequence (CR LF, bytes
    > 0x0D 0x0A, ^M^J). Unix (and Linux) uses only a \n (LF, 0x0A). The \r
    > will be read as part of the previous line then.
    >
    > There are tools to convert back and forth those formats (dos2unix and
    > unix2dos, or the crlf.py demo script in the Python source distribution).
    > But to avoid problems, it's better to use the right tools for the OS
    > you're working with (that is, don't use notepad to edit Linux files...)


    Thanks.
    See replies above. Used Gedit and ubuntu but saved in wrong format (there
    is a choice and I didn't know better).
    Phil Hansen, Jun 15, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matt
    Replies:
    9
    Views:
    402
    Andrew Thompson
    Oct 27, 2004
  2. Guest
    Replies:
    7
    Views:
    663
    Walter Wang [MSFT]
    Jun 28, 2006
  3. tconkling

    First statement always evaluated first?

    tconkling, Jul 9, 2005, in forum: C Programming
    Replies:
    3
    Views:
    312
    Keith Thompson
    Jul 9, 2005
  4. Replies:
    2
    Views:
    307
  5. Christopher M. Lusardi
    Replies:
    3
    Views:
    179
    Alan J. Flavell
    Oct 19, 2003
Loading...

Share This Page