2

使用解释器模式from gasp import *运行,但是当我将它放在脚本中时它不会。我直接从如何像计算机科学家一样思考:使用 Python 学习(标题 4.11. GASP 下)的第 4 章复制了这一点。

脚本:

from gasp import *

begin_graphics()

Circle((200, 200), 60)
Line((100, 400), (580, 200))
Box((400, 350), 120, 100)

update_when('key_pressed')
end_graphics()

终端:

ben@ubuntu:~$ python '/home/ben/Documents/Python/gasp.py' 
Traceback (most recent call last):
File "/home/ben/Documents/Python/gasp.py", line 1, in <module>
from gasp import *
File "/home/ben/Documents/Python/gasp.py", line 3, in <module>
begin_graphics()
NameError: name 'begin_graphics' is not defined
4

2 回答 2

1

重命名您的脚本。你隐藏了真正的gasp模块:

ben@ubuntu:~$ python '/home/ben/Documents/Python/gasp.py' 

当你

from gasp import *

它正在尝试import自己,因为您调用了它gasp.py

于 2011-09-17T05:07:54.367 回答
0

重命名脚本并不能解决问题。

ben@ubuntu:~$ python '/home/ben/Documents/Python/gasptest.py' 
Traceback (most recent call last):
File "/home/ben/Documents/Python/gasptest.py", line 1, in <module>
from gasp import *
File "/home/ben/Documents/Python/gasp.py", line 3, in <module>
NameError: name 'begin_graphics' is not defined

您再次包含“/home/ben/Documents/Python/gasp.py”。删除此副本:)

于 2011-09-18T07:15:35.670 回答