Python中的__future__和swagger_client问题 [英] Problems with __future__ and swagger_client in Python

查看:177
本文介绍了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定义.看来您需要使用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:

  • 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

这篇关于Python中的__future__和swagger_client问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆