Question

I'm trying to use this script on an rpi. If gpsd is running and I run the script from the linked blog post, I get the following error:

  File "/home/zzz/Timelapse/staticgps.py", line 29, in <module>
    gpsp = GpsPoller() # create the thread
  File "/home/zzz/Timelapse/staticgps.py", line 19, in __init__
    gpsd = gps.gps(mode=WATCH_ENABLE) #starting the stream of info
NameError: global name 'gps' is not defined

Any idea what's going wrong? Thanks!!

Edit: Here is my script as requested. It is a direct copy/paste from the link.

#! /usr/bin/python
# Written by Dan Mandle http://dan.mandle.me September 2012
# License: GPL 2.0

import os
from gps import *
from time import *
import time
import threading

gpsd = None #seting the global variable

os.system('clear') #clear the terminal (optional)

class GpsPoller(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        global gpsd #bring it in scope
        gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
        self.current_value = None
        self.running = True #setting the thread running to true

    def run(self):
        global gpsd
        while gpsp.running:
            gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer

if __name__ == '__main__':
    gpsp = GpsPoller() # create the thread
    try:
        gpsp.start() # start it up
        while True:
            #It may take a second or two to get good data
            #print gpsd.fix.latitude,', ',gpsd.fix.longitude,'  Time: ',gpsd.utc

            os.system('clear')

            print
            print ' GPS reading'
            print '----------------------------------------'
            print 'latitude    ' , gpsd.fix.latitude
            print 'longitude   ' , gpsd.fix.longitude
            print 'time utc    ' , gpsd.utc,' + ', gpsd.fix.time
            print 'altitude (m)' , gpsd.fix.altitude
            print 'eps         ' , gpsd.fix.eps
            print 'epx         ' , gpsd.fix.epx
            print 'epv         ' , gpsd.fix.epv
            print 'ept         ' , gpsd.fix.ept
            print 'speed (m/s) ' , gpsd.fix.speed
            print 'climb       ' , gpsd.fix.climb
            print 'track       ' , gpsd.fix.track
            print 'mode        ' , gpsd.fix.mode
            print
            print 'sats        ' , gpsd.satellites

            time.sleep(5) #set to whatever

    except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
        print "\nKilling Thread..."
        gpsp.running = False
        gpsp.join() # wait for the thread to finish what it's doing
    print "Done.\nExiting."
Was it helpful?

Solution

You didn't copy the code correctly; linked page has this line:

gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info

Note that it's just gps(), not gps.gps(); at the top of the script all names from the gps module are imported into the current namespace, making the gps() a local name.

Make sure you do have the statement from gps import * at the top of your script, from the error message it appears that you did not import it correctly (the NameError indicates that there is nothing named gps imported in your script).

OTHER TIPS

If someone stumbles here with the same problem, it is because your script file is named gps.py. Just rename it to something else, and the import will work properly.

Replace WATCH_ENABLE with 1, and it works. I'm not sure why this constant is not exposed (newbie) - it is declared in client.py of the gps module

Not only does naming your script gps.py cause gps to not be defined, you also can't name it gps1.py. Rename it to something like mygps.py

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top