Python: Running a program - Should be simple

  • Context: Python 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Program Python Running
Click For Summary

Discussion Overview

The discussion revolves around an issue encountered when trying to execute a Python script from the terminal on a Mac. Participants explore the reasons behind syntax errors that occur when running the script with the execute bit set, specifically focusing on the importance of the shebang line in script execution.

Discussion Character

  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant describes creating a Python script and successfully running it with the command python hello.py, but encountering syntax errors when executing it with ./hello.py.
  • Another participant suggests that the absence of a shebang line (#!/usr/bin/python) may cause the script to be treated as a bash script, leading to the syntax errors.
  • A participant acknowledges the suggestion and realizes that they misunderstood the shebang line, initially thinking it was just a comment.
  • Further clarification is provided about the shebang line's role in indicating the interpreter for the script, and how it is treated specially by the shell.

Areas of Agreement / Disagreement

Participants generally agree on the importance of the shebang line for script execution, with no significant disagreement noted in the discussion.

Contextual Notes

The discussion highlights the potential confusion surrounding the shebang line and its role in script execution, particularly for those less familiar with terminal commands and scripting conventions.

Saladsamurai
Messages
3,009
Reaction score
7
Hello :smile:

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

Code:
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:
python hello.py

it works great. Now I set the execute bit using
Code:
chmod +x hello.py

and when I try to run it using ./hello.py I get syntax errors?!

Code:
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!
 
Technology news on Phys.org
Perhaps without #!/usr/bin/python it is treated as a bash script? Just guessing.
 
Borek said:
Perhaps without #!/usr/bin/python it is treated as a bash script? Just guessing.

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:
 
Saladsamurai said:
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:

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
55
Views
7K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K