【问题标题】:Difficulty Importing GASP Using Script使用脚本导入 GASP 有困难
【发布时间】:2011-09-17 04:47:38
【问题描述】:

使用解释器模式,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

【问题讨论】:

  • +1 用于在不被询问的情况下包含您的第一个问题的完整回溯,因此实际上为我们提供了足够的信息来调试您的问题。

标签: python gasp


【解决方案1】:

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

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

当你

from gasp import *

它正在尝试import 本身,因为你称它为gasp.py

【讨论】:

    【解决方案2】:

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

    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”。删除此副本:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 2017-02-17
      相关资源
      最近更新 更多