【发布时间】:2021-04-06 19:23:52
【问题描述】:
我在 Linux 上运行 python 3.6。当我使用 python shell 测试代码时,它按预期工作。但是,如果从 linux 终端运行,则会出现名称错误。
#!/usr/bin/python
print("Hi")
name = input("What\'s your name?")
print (name, "is a cool name")
然后从linux终端输入名字后,我得到以下错误:
Traceback (most recent call last):
File "./test.py", line 3, in <module>
name = input("What\'s your name?")
File "<string>", line 1, in <module>
NameError: name 'Matt' is not defined
我知道如果在 python 2 上运行,你需要使用原始输入,但是这在 python 3 中没有使用。是否有另一行代码让 linux 终端接受它,比如#!/usr?
【问题讨论】:
-
你是如何运行它的?
-
/usr/bin/python --version带给你什么? -
试试:
print '{} is a cool name'.format(name) -
不,我的意思是如果你在终端中输入
/usr/bin/python --version会发生什么。看起来你正在运行 python2,即使你不打算这样做。 -
@matt-333:发生的情况是您同时安装了 python2 和 python3。我的猜测是 python3 是路径中的第一个,所以当你运行
$ python时,你正在运行 python3,但/usr/bin/python实际上是 python2。您可以使用$ which python或$ type python查看它的位置。