2012-12-22 102 views
0

我试图在rpi上使用this script。如果GPSD正在运行,并且我从链接的博客文章运行该脚本,我得到以下错误:GPS Python模块错误

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 

任何想法是怎么回事?谢谢!!

编辑:这是我的脚本按要求。它是从链接直接复制/粘贴。

#! /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." 
+0

很难说没有更多的信息。然而,你发布的链接似乎来自'gps import *',它看起来像你要么'输入gps'并使用'gps.gps'或者只是使用'gps',如果你已经完成'from gps import * ' –

+0

什么更多的信息将需要调试?在脚本上,它只是使用gps。这曾经工作,但现在已经停止,所以我猜测后端发生了一些事情,但只是不知道如何解决它。 – mh00h

+0

这个python文件的导入是什么? –

回答

2

您没有正确复制代码;链接的页面有一行:

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

请注意,这只是gps(),不gps.gps();在脚本的顶部,将gps模块中的所有名称导入到当前名称空间中,从而使gps()成为本地名称。

确保您有声明from gps import *在脚本的顶部,从错误信息看来,你没有正确导入,它(NameError表示有什么命名gps在你的脚本导入)。

0

用1替换WATCH_ENABLE,它可以工作。 我不知道为什么这个常数不暴露(新手) - 它是在gps模块的client.py中声明的