5

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?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
runnerpaul
  • 5,942
  • 8
  • 49
  • 118
  • Good question. To be honest I'm not completely sure what it is but I guess it contains packages which are not in the current Python version but which will be in a future version. I'm not a Python developer and am only seeing this for the first time so that may net even be an accurate explanation. – runnerpaul May 30 '18 at 06:46
  • You don't need `from __future__ ...` in Python 3. See [how to use from \_\_future\_\_ import print\_function](https://stackoverflow.com/questions/32032697/how-to-use-from-future-import-print-function) – Helen May 30 '18 at 07:50
  • Even if I comment that out I get `import swagger_client ModuleNotFoundError: No module named 'swagger_client'` – runnerpaul May 30 '18 at 14:38

1 Answers1

8

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
    
Helen
  • 87,344
  • 17
  • 243
  • 314
  • 1
    As a side note. The feature is named `print_function` and not `print_statement`. You are still able to import `print_function` in python3.x – MegaIng May 30 '18 at 15:14
  • @MegaIng Thanks. Looks like `import print_statement` was a typo in examples generated by Codegen - https://github.com/swagger-api/swagger-codegen/issues/5692. – Helen May 30 '18 at 15:25
  • OK, so I followed the directions above as best I could. Ended up with a folder called SwaggerPythonClient which contained a swagger_client folder containing configuration.py and rest.py so I copied the contents to my program directory. The import errors have now vanished but now I'm finding other issues. I can investigate and maybe post in a new question if required but want to check if what I did is correct before I continue. – runnerpaul May 31 '18 at 20:34
  • For my above, I ended up installing several missing packages. My application now works. – runnerpaul Jun 04 '18 at 10:28
  • I'm struggling with getting started too... would you care to explain which missing packages you also installed? – LittleNose Mar 13 '19 at 20:37