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.

3
  • 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.) Commented Apr 2, 2018 at 6:35
  • 1
    See this answer for info on the shebang line: stackoverflow.com/a/2429517/6481442 Commented Apr 2, 2018 at 6:36
  • This question also has some good information. Commented Apr 2, 2018 at 6:37

1 Answer 1

0

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

#!/usr/bin/python
#-*-coding: utf-8-*-
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.