【发布时间】:2017-11-28 13:00:26
【问题描述】:
apispy_m.py
def aspy_geolocate(api_spy_akey, api_spy_ytvid):
function_name = "geolocate"
function_description = """Get location of a youtube video!"""
if api_spy_akey == "":
errcode = 001
aspy_errhandler(function_name, errcode)
else:
pass
r = requests.get('https://www.googleapis.com/youtube/v3/videos?part=recordingDetails&id=%s&key=%s'%(api_spy_ytvid, api_spy_akey))
api_spy_jsondata = json.loads(r.text)
try:
lat = api_spy_jsondata['items'][0]['recordingDetails']['location']['latitude']
lon = api_spy_jsondata['items'][0]['recordingDetails']['location']['longitude']
alt = api_spy_jsondata['items'][0]['recordingDetails']['location']['altitude']
except KeyError:
lat = "N/A"
lon = "N/A"
alt = "M/A"
模块测试.py
import apispy_m
api_spy_akey = "*Super secret api key ;)*"
api_spy_ytvid = "*youtube vid id*"
aspy = apispy_m
aspy.aspy_geolocate(api_spy_akey, api_spy_ytvid)
print aspy.lat
print aspy.lon
print aspy.alt
运行 testmodule.py 后出错(使用 Python2):
Traceback (most recent call last):
File "test.py", line 6, in <module>
print aspy.lat
AttributeError: 'module' object has no attribute 'lat'
我似乎无法解决此错误。我已经尝试过from apispy_m import * 和from apispy_m import aspy_geolocate。这是我第一次创建 python 模块。
我们将不胜感激!
【问题讨论】:
-
lat是一个局部变量。让你的函数返回那些值(作为一个元组)。
标签: python python-2.7 printing module attributeerror