使用脚本导入GASP时遇到困难

2 投票
2 回答
518 浏览
提问于 2025-04-17 02:28

在解释器模式下,from gasp import * 可以正常运行,但当我把它放到脚本里就不行了。我是直接从《如何像计算机科学家一样思考:用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

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

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"这个路径。把这个重复的部分删掉吧 :)

1

把你的脚本改个名字。你把真正的 gasp 模块给隐藏起来了:

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

当你

from gasp import *

时,它试图去 import 自己,因为你把它叫做 gasp.py

撰写回答