难以使用脚本导入GASP

时间:2011-09-17 04:47:39

标签: python gasp

使用解释器模式,from gasp import *会运行,但是当我将它放在脚本中时,它不会运行。我正在从How to Think Like a Computer Scientist: Learning with Python的第4章(标题4.11。GASP下)直接复制这一点。

脚本:

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

2 个答案:

答案 0 :(得分:1)

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

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

当你

from gasp import *

它正在尝试import本身,因为您将其称为gasp.py

答案 1 :(得分: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”。删除此副本:)