Python 中 __future__ 和 swagger_client 的问题

run*_*aul 4 python python-import python-3.x swagger strava

Strava API 文档提供了以下示例代码,我复制并输入了我自己的访问令牌和俱乐部 ID:

from __future__ import print_statement
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: strava_oauth
swagger_client.configuration.access_token = 'MY_ACCESS_TOKEN'

# create an instance of the API class
api_instance = swagger_client.ClubsApi()
id = MY_CLUB_ID # Integer | The identifier of the club.
page = 56 # Integer | Page number. (optional)
perPage = 56 # Integer | Number of items per page. Defaults to 30.     (optional) (default to 30)

try:
    # List Club Activities
    api_response = api_instance.getClubActivitiesById(id, page=page, perPage=perPage)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling ClubsApi->getClubActivitiesById: %s\n" % e)
Run Code Online (Sandbox Code Playgroud)

我试着运行它我得到

from __future__ import print_statement
SyntaxError: future feature print_statement is not defined
Run Code Online (Sandbox Code Playgroud)

我也可以看到我的swagger_client进口也会得到同样的结果。我试过为每个安装软件包,但这没有任何区别。我读到__future__我应该在 > Python 2.7 但我目前使用的是 3.6。

我该如何解决这个问题?

Hel*_*len 7

1) 第一行包含一个错字

from __future__ import print_statement
                             ^^^
Run Code Online (Sandbox Code Playgroud)

它应该是

from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)

但由于您使用的是 Python 3,您实际上并不需要此导入 -有关详细信息,请参阅此问答

2)swagger_client可能是从Strava OpenAPI 定义生成的 Python 客户端。看起来您需要使用 Swagger Codegen 手动生成它。做这件事有很多种方法: