I have the following program:
#!/usr/local/bin/python3
print(\"Hello\")
Via terminal I do test.py
and I get:
Another reason can be the presence of the byte order mark (BOM) at the start of the file, if the file is using an Unicode encoding.
The presence of the BOM was my problem for my Python script encoded in UTF-8. I removed the BOM using my text editor Geany, but Notepad++ can also remove it, and I was able to run my script with a starting shebang line with ./myscript.py .
To quote Wikipedia :
The Unicode Standard permits the BOM in UTF-8, but does not require or recommend its use. Byte order has no meaning in UTF-8 [...]