【发布时间】:2019-06-08 00:44:37
【问题描述】:
我编写了一个脚本并将其保存为 test.py(代码如下所示)。当我从脚本(运行模块 5)运行它时,我会在 Python Shell 中得到结果。
但我已经尝试了多种在线可用的建议,让它从 Python Shell 运行,而不是我失败了(下面粘贴了一个错误)。
如何从 python shell 运行 python 脚本? 我正在运行的 Python 版本是 3.7.3,并且在 Windows 中。
#!/usr/bin/python3
print(" Hello, world!")
exec(open(test.py).read())
输出:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
exec(open(test.py).read())
NameError: name 'test' is not defined
【问题讨论】:
-
永远不要使用
exec()。只需从文件中删除该行即可。 -
只做“导入测试”。
-
@MisterMiyagi 在 cmets 中使用反引号。
-
@Code-Apprentice 许多移动键盘不能“正确”操作。
-
@MisterMiyagi Gotchya!我的 android 键盘有一个反引号键,但我花了很长时间才找到它。我敢肯定,美国以外的键盘应用程序会更加困难
标签: python