Skip to main content
 首页 » 编程设计

python之我在使用 Uber Python API 时遇到问题

2024年10月24日11bjzhanghao

我在 python 中编码,使用 2.6 版,使用 Uber API,当我尝试导入库 uber_rides.auth 时,它抛出此错误:

Traceback (most recent call last): 
  File "C:\Inetpub\vhosts\underdevelopment.biz\httpdocs\web\webtemp3\uber\socket.py", line 4, in <module>     
    from uber_rides.auth import AuthorizationCodeGrant 
  File "C:\Inetpub\vhosts\underdevelopment.biz\httpdocs\web\webtemp3\uber\uber_rides\auth.py", line 133 
    query_params = [qp: query_params[qp][0] for qp in query_params] 
                      ^ 
SyntaxError: invalid syntax 

我的脚本原代码是这样的:

print('Content-Type: text/plain') 
print('') 
from uber_rides.auth import AuthorizationCodeGrant 
def main(): 
    auth_flow = AuthorizationCodeGrant( 
        'xxxxxx-xxxxxxx', 
        'xxxxx-xxxxx', 
        'xxx-xxxxxxxxx', 
        '', 
    ) 
    auth_url = auth_flow.get_authorization_url() 
 
if __name__ == "__main__": 
    main() 

似乎错误来自库,但我还找不到它。

请您参考如下方法:

是的,这是无效的 Python 语法。但是,不清楚您是如何得到该文件的。

某人或某物更改了该文件。那不是 original source code as distributed by Uber ,其中该行使用正确的语法来理解字典:

query_params = {qp: query_params[qp][0] for qp in query_params} 

重新安装工程,错误不在上游

请注意,以上语法仅适用于 Python 2.7 及更高版本。您可以尝试将其替换为带有生成器表达式的 dict() 调用,请参阅 Alternative to dict comprehension prior to Python 2.7 :

query_params = dict((qp, query_params[qp][0]) for qp in query_params) 

考虑到代码可能存在其他问题,升级到 Python 2.7 可能是更好的选择。