Search code examples
pythongoogle-app-enginehttplib2

Google App Engine httplib2 import Http results in Syntax Error


Looking for some help with Google App Engine. Below is the offending part of my code:

from __future__ import print_function
import webapp2
import jinja2
import csv
import os
from collections import namedtuple
from httplib2 import Http
from googleapiclient.discovery import build
from oauth2client import file, client, tools
import datetime
##code to do stuff

Where I'm running into trouble is with the "from httplib2 import Http" line. I'm getting the following error:

Traceback (most recent call last):
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/c3b7fd7c606f3aa7/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/c3b7fd7c606f3aa7/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/c3b7fd7c606f3aa7/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/k~fraseline2019/20181014t160622.413269158822473824/main.py", line 10, in <module>
    from httplib2 import Http
  File "/base/data/home/apps/k~fraseline2019/20181014t160622.413269158822473824/lib/httplib2/__init__.py", line 382
    print('%s:' % h, end=' ', file=self._fp)
                        ^
SyntaxError: invalid syntax

I've got the httplib2 module files in my app directory. I've been googling around but nobody seems to have had this issue - can someone help?

THANK YOU!


Solution

  • Please read about MCVEs. It would appear that the MCVE for this issue is one line:

    from httplib2 import Http

    I believe your issue is that your httplib2 module lacks the needed future import.

    from __future__ import print_function
    

    Future imports only apply to the module in which they appear. Without it, in 2.7, one gets the traceback you got.

    >>> print(1, 2, end='')
      File "<stdin>", line 1
        print(1, 2, end='')
                       ^
    SyntaxError: invalid syntax