Python中的__future__和swagger_client问题

问题描述:

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

The Strava API documentation gives the following sample code which I copied and entered my own access token and club 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.

I can also see that I will get the same with my swagger_client imports. I've tried installing packages for each but this hasn't made any difference. I read that for the __future__ I should be on > Python 2.7 but I'm currently using 3.6.

如何解决此问题?

1)第一行包含错字

from __future__ import print_statement
                             ^^^

应该是

from __future__ import print_function

但是由于您使用的是Python 3,因此实际上并不需要此导入-请参阅此问题与解答以了解详细信息.

But since you are using Python 3, you don't actually need this import - see this Q&A for details.

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

2) swagger_client is probably the Python client generated from the Strava OpenAPI definition. It looks like you need to generate it manually using Swagger Codegen. There are several ways to do this:

  • 将Strava OpenAPI定义粘贴到 https://editor.swagger.io 并选择 Generate客户端> P​​ython .
  • 安装命令行版本 ="https://github.com/swagger-api/swagger-codegen" rel ="nofollow noreferrer">摇摇Codegen 并运行:

  • Paste the Strava OpenAPI definition into https://editor.swagger.io and select Generate Client > Python.
  • Install the command-line version of Swagger Codegen and run:

# 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