0

My python version is

Python 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information.

This is my python script:

  def main():
          print " Generating Initial data"
  if __name__ == '__main__':
          main()

If i run it as,

/test$ python gen_in_data.py

 Generating Initial data

it is working fine.

But if I run it as ,

/test$ ./gen_in_data.py
./gen_in_data.py: line 1: syntax error near unexpected token `('
./gen_in_data.py: line 1: `def main():'

it is throwing this error. Please let me know the issue.

Abdullah Ahmed Ghaznavi
  • 1,978
  • 3
  • 17
  • 27
dharma
  • 13
  • 5
  • 1
    This error almost certainly means your shell is running the script, rather than the Python interpreter. By default, executable script are run by the shell; you need a shebang line to specify a different interpreter—in this case, probably `/usr/bin/env python` or less commonly a hardcoded path like `/usr/local/bin/python`. (I'm sure this is a dup of an existing question, which will have a more detailed answer that explains things better… just let me find it.) – abarnert Apr 02 '18 at 06:35
  • 1
    See this answer for info on the shebang line: https://stackoverflow.com/a/2429517/6481442 – Jonathan Dayton Apr 02 '18 at 06:36
  • [This question](https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take) also has some good information. – abarnert Apr 02 '18 at 06:37

1 Answers1

0

add this at the beginning of the file it is a special comment

#!/usr/bin/python
#-*-coding: utf-8-*-
Walle Cyril
  • 3,087
  • 4
  • 23
  • 55
  • 2
    Why? To make sure that his script won't run if Python is in `/usr/local/bin`, and since nobody's explained to him what this means, he'll be unable to debug it? Or to make sure it's an error if his code isn't in UTF-8? – abarnert Apr 02 '18 at 06:39