我是 Python 新手,在执行我的输入代码时遇到问题。我的代码如下:
userInput = input("Hi, What is your name? ")
print("Welcome {0}, Nice to meet you!".format(userInput))
现在的问题是它在 中运行得很好PyCharm 2020
,但是当我尝试在我Ubuntu 20.04
的终端中运行它时,它返回以下错误:
./userIO.py: line 7: syntax error near unexpected token `('
./userIO.py: line 7: `userInput = input("Hi, What is your name? ")'
我想知道可能是什么原因。你能帮我找出导致问题的原因吗?
运行脚本时(例如通过./script
命令行执行),Linux 使用称为shebang 行的特殊行来确定使用什么程序来运行脚本。例如,#!/bin/bash
对于 Bash 脚本或#!/usr/bin/env python3
Python 脚本。
如果没有 shebang 行,Bash 将默认在 Bash 中运行脚本(即作为 shell 脚本)。由于您的脚本不是 shell 脚本,因此 Bash 尝试将您的 Python 代码解释为 shell 脚本时会出现一堆错误。
要解决此问题,您需要一个 shebang 行,它必须是脚本的第一行。对于 Python 3 脚本,您可以使用#!/usr/bin/env python3
, 例如如下:
#!/usr/bin/env python3
userInput = input("Hi, What is your name? ")
...
或者,您可以通过使用 Python 解释器显式运行脚本来解决此问题,例如python3 script.py
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句