Search code examples
pythonpython-2.7google-apigoogle-analytics-apigoogle-api-python-client

How to fix 'print("%s:" % h, end=" ", file=self._fp)' SyntaxError: invalid syntax in python2.7


I try to connect Google Analytics Reporting API v4 using python 2.7.

when I use this :

from googleapiclient.discovery import build

from oauth2client.service_account import ServiceAccountCredentials

error messege : 
raceback (most recent call last):
  File "C:\Users\Katrina\PycharmProjects\inventory\HelloAnalytics.py", line 4, in <module>
    from googleapiclient.discovery import build
  File "C:\Python27\lib\googleapiclient\discovery.py", line 52, in <module>
    import httplib2
  File "C:\Python27\lib\httplib2\__init__.py", line 475
    print("%s:" % h, end=" ", file=self._fp)
                        ^
SyntaxError: invalid syntax

I tried also with


from apiclient.discovery import build

instead googleapiclient and got the same error.

running Python 2.7.13, pip 19.1.1

https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py


Solution

  • The Hello Analytics Reporting API v4; Python quickstart for service accounts quick start is intened for use with Python 3

    This is because the Google API python client library supports

    Python 3.4, 3.5, 3.6 and 3.7 are fully supported and tested. This library may work on later versions of 3, but we do not currently run tests against those versions

    you will need to upgrade your version of python to use it.

    That being said the library does state that 2.7 is just deprecated so i wonder if this note should be removed or if there is in fact a way for you to alter the quick start to "make it work". (Note issue on form about the 2.7 deprecation )

    Note from issue form

    Python 2.7 is deprecated, but we intend to drop support for Python 2.7 on January 1, 2020.

    The stack trace points to an incompatibility in httplib2. httplib2 started releasing py3 wheels on PyPI a few months ago. (starting with 0.12.3)

    The python2 version appears to be available in the .tar.gz. If they install from the tarfile or pin an older version of httplib2 the library should still work with Python 2.7.