Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python: Running a program - Should be simple

  1. Dec 29, 2012 #1
    Hello :smile:

    As strange thing is happening. I have created a simple file using emacs (on a Mac) that contains the following:

    Code (Text):
    def main():

        print "hello!!!"

    if __name__ == "__main__":
        main()
     
    And I have saved it in the working directory as hello.py

    In a terminal window, if I type
    Code (Text):
    python hello.py
    it works great. Now I set the execute bit using
    Code (Text):
    chmod +x hello.py
    and when I try to run it using ./hello.py I get syntax errors?!

    Code (Text):
    Python_Projects saladsamurai$ ./hello.py
    ./hello.py: line 1: syntax error near unexpected token `('
    ./hello.py: line 1: `def main():'
     
    Any ideas on this? Thanks!
     
  2. jcsd
  3. Dec 29, 2012 #2

    Borek

    User Avatar

    Staff: Mentor

    Perhaps without #!/usr/bin/python it is treated as a bash script? Just guessing.
     
  4. Dec 29, 2012 #3
    Borek! Thanks! That what exactly it! I am not too good at terminal or Python. I saw the #!/usr/bin/python in the example file and I thought it was a comment :redface:
     
  5. Dec 29, 2012 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It is a python comment. I think it would even be a shell script comment if it were anywhere but the first line.

    But your shell treats the first line specially if it starts with #!, and interprets it as a path to something to execute to interpret the script.

    This is why scripting languages all use # to start comments: so that this convention your shell uses can be used without confusing your scripting language.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Python: Running a program - Should be simple
  1. Run Python on Windows (Replies: 6)

  2. Programming in Python (Replies: 22)

  3. Simple python question (Replies: 2)

Loading...