学习python练习1文件无法在term中运行

2024-10-03 04:28:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我到处寻找答案,似乎这是一个简单的问题。唉,没有骰子。在

问题:我无法从终端“运行”python文件(macosx10.9.2)

  • 运行python返回:

Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >;>>

  • 尝试从终端运行python文件
  • 浏览到创建了“pthw_e01.py”的文件后:

    $python pthw_e01.py
    $

总之,当我尝试运行python脚本时,什么都不会发生(我相信这是一个脚本,不是一个文件吗?)。在

如果有助于您回答,我在终端中尝试运行的python只是一堆print语句(见下文):

print "Hello World!" print "Hello Again" print "I like typing this." print "This is fun." print 'Yay! Printing.' print "I'd much rather you 'not'." print 'I "said" do not touch this.'

Tags: 文件答案py脚本终端applehellonot
3条回答

您可能是在打开文件时保存的,文件是空的,然后在键入文本后不再保存文件。检查你的文件大小,看看它是否为零。再次保存并重新运行。每次进行更改和运行时都必须保存它

如果它们在一个大脚本中,则需要将它们放在单独的行中,或者在shell中分别输入每一行。Python通常使用新行来确定每个语句的结束位置。在

pthw_e01.py:

print "Hello World!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'

从上面的屏幕截图来看,您试图在解释器中运行python文件,而不是在终端中实际运行程序。如果您愿意,您可以在解释器中运行每个打印行,但不能运行整个文件。不只是键入python并打开解释器,而是键入python pthw_e01.py,它应该会将您的程序显示到终端。在

相关问题 更多 >