Problems with __future__ and swagger_client in Python

女生的网名这么多〃 提交于 2019-12-08 12:43:37

问题


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)

I try to run it I get

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

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.

How do I resolve this issue?


回答1:


1) The first line contains a typo

from __future__ import print_statement
                             ^^^

it should be

from __future__ import print_function

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

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
    


来源:https://stackoverflow.com/questions/50597854/problems-with-future-and-swagger-client-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!