[Tutor] Problems with Importing into the Python Shell
Steven D'Aprano
steve at pearwood.info
Sat Jun 12 10:33:23 CEST 2010
On Sat, 12 Jun 2010 02:50:26 pm Andrew Martin wrote:
> >>> from chap03 import *
>
> Traceback (most recent call last):
> File "<pyshell#8>", line 1, in <module>
> from chap03 import *
> File "C:/Python26\chap03.py", line 2
> print param param
> ^
> SyntaxError: invalid syntax
This tells you exactly what the error is: a syntax error in your module
chap03, which prevents Python from importing it.
`print a b` is not valid Python syntax. Python can't correct the error,
because it doesn't know if you mean print param+param, or param,param
or "param param" or something else, and it refuses to guess.
> The chap03.py file is a simple one that looks like this:
> def print_twice(param):
> print param param
The print statement takes a comma-separated list of things to print:
print param, param
> My second problem is that I need to install and import GASP in order
> to follow the tutorial. When I tried to do import it, I ran into an
> error like
>
> this:
> >>> from gasp import *
>
> Traceback (most recent call last):
> File "<pyshell#9>", line 1, in <module>
> from gasp import *
> File "C:\Python26\lib\site-packages\gasp\__init__.py", line 1, in
> <module> from api import *
> File "C:\Python26\lib\site-packages\gasp\api.py", line 1, in
> <module> import backend
> File "C:\Python26\lib\site-packages\gasp\backend.py", line 7, in
> <module> except ImportError: raise 'Pygame is not installed. Please
> install it.' TypeError: exceptions must be old-style classes or
> derived from BaseException, not str
Here you have two problems. The first problem is that you need to have
Pygame installed, and you don't, or it is installed in such a place
that Python 2.6 can't see it.
The second is that the version of gasp you are using has a bug in it,
possibly because you're using an old version. In the very early days of
Python, it used string exceptions: to raise an error message, you would
say:
raise "this is an error message"
For various reasons, this was not very satisfactory, and over the years
these string exceptions were discouraged, then flagged as officially
deprecated, then Python would print an warning message when you used
them, and finally in Python 2.6 they because prohibited:
>>> raise "this is an error"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: exceptions must be classes or instances, not str
So the version of gasp you are using pre-dates Python 2.6, and contains
obsolete code that does not run correctly. Your work-arounds, in order
of best-to-worst ideas:
(1) Install an updated version of gasp that works with Python 2.6;
(2) Install Python 2.5 and use that; or
(3) Hack your copy of gasp to fix the bug.
Of course, since you're a beginner, option 3 (not very desirable at the
best of times!) is virtually impossible. Good luck!
However, there is one little ray of sunshine. If you install Pygame,
gasp should successfully import it and therefore not try to raise a
string exception, and the second error will disappear all on its own.
(But who knows how many more little landmines are waiting...)
--
Steven D'Aprano
More information about the Tutor
mailing list