I was facing above issue while running a python script using ansible in linux machine. Following solution, I found on Stackoverflow

Short answer: Either run these as python filename.py, or else add the line #!/usr/bin/python to the top of your Python scripts.

Long answer: When you run a file from the command line in Linux (which is what the Raspberry Pi is running), by default it assumes that the file is a shell script file (usually Bash script). So it uses the Bash shell (or some other shell, but it’s usually Bash) to interpret the file, and Bash doesn’t know Python syntax. If you want to run your file using a different interpreter (Python, in this case), you have to add a “magic line” at the top of the file starting with #! (usually pronounced "hash-bang", and sometimes pronounced "shebang" for short). Following the #! characters is the full path of the interpreter to use, e.g. /usr/bin/python for Python scripts. (You can also use /usr/bin/env python as another answer suggested; I prefer /usr/bin/python because it's impossible to get the wrong Python interpreter that way. But that's getting into advanced topics that may be more than you need right now.)

So when you put the line #!/usr/bin/python at the top of your Python scripts, you're telling the Linux system which interpreter to run the program with, and then it should All Just Work™.