2

When I put this python code into the REPL for python (the interactive shell), it works as expected:

>>> def get_header():
...     return (None,None,None)
... 
>>> get_header()
(None, None, None)

Note that the return statement is indented by four spaces, and I have checked to ensure there are no extraneous spaces.

when I put the exact same code into a python script file and execute it, I get the following error:

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

WHY?

EDIT: this is the exact contents of test.py, white spaces and all:

def get_header():
    return (None,None,None)

get_header()

I have verified that the above script (test.py) does yield the above error as it above stands.

4
  • 1
    Please post the exact contents of test.py, indentation and all. Commented Dec 28, 2011 at 16:55
  • Maybe something in lines 1-4? Commented Dec 28, 2011 at 16:56
  • I call shenanigans. Post at least lines 1 through 6 of your test.py file. Commented Dec 28, 2011 at 17:06
  • call it with python test.py . As one of the answers suggests, you are missing the shebang line Commented Dec 28, 2011 at 17:14

1 Answer 1

11

The reason this is not working is that you don’t have anything telling bash that this is a Python script, so it tries to execute it as a shell script, then throws an error when the syntax isn’t right.

What you need is to start the file with a shebang line, telling it what it should be run with. So your file becomes:

#!/usr/bin/env python

def get_header():
    return (None, None, None)

print get_header()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.