简体   繁体   English

意外令牌附近的Python语法错误

[英]Python syntax error near unexpected token

I checked several duplicate questions, but they are not related to python. 我检查了几个重复的问题,但它们与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. 一旦我尝试从终端(bash shell)使用它,就会收到语法错误。

./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). 查看代码似乎没有什么错,而且我也没弄清楚是哪一个是问题,以及它是否与Python或Linux有关(就像在其他文章中所说的,它也可能取决于Linux)。

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. 在第一行中,这只是一个注释,整个内容都被解释为sh脚本。 It should look like this: 它看起来应该像这样:

#!/bin/python3

Change first string to something like #!/usr/bin/python3 将第一个字符串更改为#!/usr/bin/python3

You should use #! 您应该使用#! instead of # . 代替#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM