Python Python: Running a program - Should be simple

AI Thread Summary
A user encountered a syntax error when trying to execute a Python script named hello.py after setting the execute bit with chmod +x. The script runs correctly with the command python hello.py but fails with ./hello.py, producing a syntax error related to the first line of the script. It was clarified that the absence of a shebang line (#!/usr/bin/python) at the beginning of the script causes the shell to misinterpret the file as a bash script rather than a Python script. The shebang line is crucial as it tells the shell which interpreter to use for executing the script. This discussion highlights the importance of including the shebang line in executable scripts to ensure proper execution.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top