Python syntax error near unexpected token

Question:

I checked several duplicate questions, but they are not related to python.

This is my script

#/bin/python3
import time

# Get the today date and use it as folder name
print(time.strftime("%Y-%m-%d"))

as soon as I try to use it form the terminal (bash shell), I get the syntax error.

./test.py: line 5: syntax error near unexpected token `time.strftime'
./test.py: line 5: `print(time.strftime("%Y-%m-%d"))'

Looking at the code nothing seems to be wrong, and I’m not getting which one is the problem and if it’s related to Python or to Linux (as in other posts seems it can depends from linux too).

Asked By: Luigi T.

||

Answers:

You’re missing the ! in the first line, so it’s just a comment and the whole thing is being interpreted as a sh script. It should look like this:

#!/bin/python3
Answered By: Izkata

Change first string to something like #!/usr/bin/python3

You should use #! instead of #.

Answered By: Ardling

why is this line necessary?
#/bin/python3
i was running python scripts without it no problem
using linux mint

Answered By: plasticlax
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.