由于某些原因,我无法将变量从一个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