4

why this line #!flask/bin/python is added on the top of this code?

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello, World!"

if __name__ == '__main__':
    app.run(debug=True)

I got following error when I removed it

from: can't read /var/mail/flask
./app.py: line 3: syntax error near unexpected token `('
./app.py: line 3: `app = Flask(__name__)'
Raghav Mittal
  • 210
  • 3
  • 9
  • I can't see that line in the code posted. – too honest for this site Jun 20 '15 at 13:39
  • It’s a shebang, and it tells the shell that when you invoke this file as `./app.py`, it should be run with the Python interpreter at flask/bin/python. Since it’s missing, it tries to run the file with the default shell (which is probably bash, and which will choke on a .py file). – alexwlchan Jun 20 '15 at 13:42

1 Answers1

5

#! is a shebang. On UNIX/UNIX like operating systems it basically tells your shell which executable (python in your case) to execute the script with. Without it, the shell is executing the script directly and since it does not understand Python code, it raises an error.

uname01
  • 1,221
  • 9
  • 9