-1

python/spyder 的新手。我无法按照我想要的方式运行脚本。使用以下脚本的快速示例:

# Demo file for Spyder Tutorial
# Hans Fangohr, University of Southampton, UK

def hello():
    """Print "Hello World" and return None"""
    print("Hello World")

# main program starts here
hello()

我已将此保存为 hello.py。当我在命令行中输入 hello() 时,出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined

但是,如果我在编辑器中打开此脚本的情况下点击运行按钮,它运行得很好,并打印 Hello World。然后我可以在我的命令行中输入 hello() 并且它运行得很好。

有人可以向我解释为什么会这样吗?

我的总体目标是保存一个可以从默认 cwd 运行的 startup.py 脚本,它将我的 cwd 更改为我想要保存所有代码的位置。

4

1 回答 1

2

hellodef hello在您执行语句之前未定义。您尚未运行脚本,因此该行尚未执行。运行脚本后,hello就已经定义好了。

于 2016-08-10T20:54:49.973 回答