pythongasp

Difficulty Importing GASP Using Script


Using interpreter mode, from gasp import * runs, but when I place it in a script it doesn't. I'm copying this straight from chapter 4 of How to Think Like a Computer Scientist: Learning with Python (under heading 4.11. GASP).

Script:

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()

Terminal:

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

Solution

  • Rename your script. You're hiding the real gasp module:

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

    When you

    from gasp import *
    

    it's trying to import itself because you called it gasp.py.