Python 中 __future__ 和 swagger_client 的问题

2024-05-08 03:09:37 发布

您现在位置: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)

我试着跑我明白了

^{pr2}$

我还可以看到,我的swagger_client导入也会得到相同的结果。我没试过安装这些软件包,但都没用。我读到,对于__future__,我应该使用python2.7,但我目前使用的是3.6。在

我如何解决这个问题?在


Tags: ofinstancefromimportclientapiidswagger
1条回答
网友
1楼 · 发布于 2024-05-08 03:09:37

1)第一行包含排印错误

from __future__ import print_statement
                             ^^^

应该是的

^{pr2}$

但是,由于您使用的是Python3,所以实际上并不需要这个导入—有关详细信息,请参见this Q&A。在

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

  • 将stravaopenapi定义粘贴到https://editor.swagger.io中,然后选择Generate Client>;Python。在
  • 安装Swagger Codegencommand-line version,然后运行:

    # 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
    

相关问题 更多 >