Python 中 __future__ 和 swagger_client 的问题

Problems with __future__ and swagger_client in Python

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)

我尝试 运行 我得到

from __future__ import print_statement
SyntaxError: future feature print_statement is not defined

我还可以看到,我的 swagger_client 导入也会得到相同的结果。我已经尝试为每个安装包,但这没有任何区别。我读到 __future__ 我应该使用 > Python 2.7 但我目前使用的是 3.6.

如何解决这个问题?

1) 第一行有错别字

from __future__ import print_statement
                             ^^^

应该是

from __future__ import print_function

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

2) swagger_client 可能是从 Strava OpenAPI definition 生成的 Python 客户端。看起来您需要使用 Swagger Codegen 手动生成它。有几种方法可以做到这一点:

  • 将 Strava OpenAPI 定义粘贴到 https://editor.swagger.io 和 select 生成客户端 > Python.
  • 安装 command-line version of Swagger Codegen 和 运行:

    # Windows
    java -jar swagger-codegen-cli-<ver>.jar generate -i https://developers.strava.com/swagger/swagger.json -l python -o ./StravaPythonClient
    
    # Mac
    swagger-codegen generate -i https://developers.strava.com/swagger/swagger.json -l python -o ./StravaPythonClient