由於某些原因,我無法將變量從一個Python文件傳遞到另一個文件。請參閱下面的文件。變量不會在Python腳本之間傳遞
pullgps.py
:
from lenny1 import *
import time
while (1):
print lenny1.lat
print lenny1.lon
print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
time.sleep(6)
lenny1.py
:
import gps
# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
while True:
try:
report = session.next()
# Wait for a 'TPV' report and display the current time
# To see all report data, uncomment the line below
# print report
if report['class'] == 'TPV':
if hasattr(report, 'lat'):
lat = report.lat ### export to pullgps
if hasattr(report, 'lon'):
lon = report.lon ### export to pullgps
except KeyError:
pass
except KeyboardInterrupt:
quit()
except StopIteration:
session = None
print "GPSD has terminated"
lenny.py
當我打印report.lon
和report.lon
工作在自己的罰款。只是無法將變量導出到pullgps.py
。它應該很簡單,但由於某些原因,變量不會通過。謝謝!
我想你應該閱讀[this](https://docs.python.org/3/tutorial/modules.html)關於import語句。 – user3591723