tags:

views:

82

answers:

2

I'm testing out some of the examples in Rapid GUI Programming with Python and Qt, but running into a stumbling block here or where. When I copied to following exercise (verbatim, from the book):

import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication(sys.argv)

try:
    due = QTime.currentTime()
    message = "Alert!"

    if len(sys.argv) < 2:
        raise ValueError

    hours, mins = sys.argv[1].split(":")
    due = QTime(int(hours), int(mins))

    if not due.isValid():
        raise ValueError
    if len(sys.argv) > 2:
        message = " ".join(sys.argv[2:])

except ValueError:
    message = "Usage: alert.pyw HH:MM [optional message*]" # 24hr Clock

while QTime.currentTime() < due:
    time.sleep(20) # 20 seconds

label = QLabel("<font color=red size=72><b>" + message + "</b></font>")

label.setWindowFlags(Qt.SplashScreen)
label.show()

QTimer.singleShot(60000, app.quit) # 1 minute

app.exec_()

I get the following error:

andy@ASUSix:~/Documents/Programming/Python/PyQt$ from: can't read /var/mail/PyQt4.QtCore
from: can't read /var/mail/PyQt4.QtGui
./alert.pyw: line 6: syntax error near unexpected token `('
./alert.pyw: line 6: `app = QApplication(sys.argv)

What's going wrong here? Is my PATH set up incorrectly?

+3  A: 

You probably forgot to add a shebang to your script, to tell your shell to actually run it with the Python interpreter. Try adding

#!/usr/bin/python

as the first line in your script, provided that's where your Python interpreter is installed. You might want to try

which python

in case you're not sure.

Jim Brissom
`#!/usr/bin/env python` will find the location of python in `$PATH`, while `#!/usr/bin/python` only works if python is in `/usr/bin`. Still +1 to both.
rebus
I know. Yet, there are some (albeit rare) distros who don't feature /usr/bin/env, and some brands of BSD behave differently, too. All in all, I tried to focus on the issue here and point to the source of error. You are right to mention that I could've pointed out that using env is an option, though.
Jim Brissom
+2  A: 

In my Windows 7 and Linux machine it works perfectly. Post the content of your PATH variable and add /usr/bin/env python as the first line to your script. Also try to run it using python script.py. Does it work?

Yassin