25

after runing this code,I found import error:-

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

application = webapp.WSGIApplication([('/', MainPage)],debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

how to use google.apengine.ext

Marcin Pietraszek
  • 3,134
  • 1
  • 19
  • 31
Rajeev Das
  • 1,581
  • 4
  • 18
  • 21
  • How did you install it? Check that the it's on your PYTHONPATH. – StoryTeller - Unslander Monica Apr 03 '13 at 09:43
  • 1
    If you are trying to run it in interactive Python environment, you will have to modify sys.path as mentioned in comments below. If you plan to serve the web application then you should run your app using dev_appserver.py. Command would be /platform/google_appengine/dev_appserver.py app.yaml – Akshar Raaj May 02 '17 at 15:37
  • I don't know why you removed my answer. I had exactly the same error and having file google.py caused this problem. So for me is answer. May be is not answer to your exact case, but it was answer to my case. Also I had absolutely the same message as error : "python import error “No module named appengine.ext” , so please return my answer back. Regards. – makkasi Jul 25 '17 at 06:29

8 Answers8

20
import sys
sys.path.insert(1, '/Users/<username>/google-cloud-sdk/platform/google_appengine')
sys.path.insert(1, '/Users/<username>/google-cloud-sdk/platform/google_appengine/lib/yaml/lib')
sys.path.insert(1, 'lib')

if 'google' in sys.modules:
    del sys.modules['google']

this solves the problems for me

varun
  • 4,522
  • 33
  • 28
  • Where do you put this code exactly? Is it in the `appengine_config.py` file or elsewhere? – Y2H Feb 02 '18 at 11:19
  • This code is to be placed at the beginning of you main execution method, it is adding reference to GAE package . This is also an old code, things have changed since I last wrote it. – varun Feb 02 '18 at 11:28
18

It looks like the App Engine SDK is not installed, or at least the Python runtime cannot find it.

read and follow the instructions here: https://cloud.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python

They tell you, how to install App Engine SDK for Python.

BrettJ
  • 6,801
  • 1
  • 23
  • 26
Jörg Beyer
  • 3,631
  • 21
  • 35
  • 13
    Hey, I followed the instructions on the link you specified, still the error is there. – Namita Maharanwar Mar 01 '16 at 06:03
  • 7
    @NamitaMaharanwar I ran the following command through Terminal (I'm using Mac) : `export PYTHONPATH="$PYTHONPATH:/usr/local/google_appengine:/usr/local/google_appengine/lib/:/usr/local/google_appengine/lib/yaml/"`. You have to configure the paths to the libraries. – Jaqueline Passos Mar 02 '16 at 13:42
  • Yes, exactly. Setting PYTHONPATH was missing in steps that I followed. Done. – Namita Maharanwar Mar 03 '16 at 08:15
  • 1
    @JaquelinePassos don't quite understand why but setting `PYTHONPATH` was the only thing that worked for me as well. – Willian Fuks Oct 21 '17 at 21:16
  • 1
    still not working after pasting export PYTHONPATH="$PYTHONPATH:/usr/local/google_appengine:/usr/local/google_appengine/lib/:/usr/local/google_appengine/lib/yaml/" – Seunghun Sunmoon Lee Aug 01 '18 at 10:21
  • 1
    here is another fix: look at `import google` `print(google.__path__)`. Start python with -sS if it is using site packages and dist packages that lack appengine, or just delete those `google` folders – Gavin Haynes Jan 15 '19 at 19:18
10

Try:

import google
print google.__path__

to see what exactly you're importing.

Xavi Gonzalvo
  • 149
  • 1
  • 3
  • 1
    >>> import google Traceback (most recent call last): File "", line 1, in ImportError: No module named google >>> print google.__path__ Traceback (most recent call last): File "", line 1, in NameError: name 'google' is not defined – TSR May 08 '17 at 00:20
  • This helped me. It pointed me to the `protobuf` package from Homebrew, which installs its own version of the `google` module. Removing that fixed the problem. – Steadicat Jun 06 '18 at 22:32
  • I tried the above and got ['C:\\Python27\\lib\\site-packages\\google'] That isn't the right place. I then using the tip below put this code at the top of my Pycharm GAE test file: import sys if 'google' in sys.modules: del sys.modules['google'] And that fixed my issue. Now to learn why. https://justus.science/blog/2015/04/19/sys.modules-is-dangerous.html – Brian Kay Walker Oct 23 '18 at 15:19
  • 1
    So I watched David Beazley's Modules and Packages video from Pycon. After doing the import google and printing trick I found google pointing to the site-packages. In there I found a file ending in .pth (these files can modify your path) it was called google_auth-1.5.1-py3.6-nspkg.pth I changed the extension name to .#pth so it didn't load and all worked. It now points to 'C:\\Program Files (x86)\\Google\\Cloud SDK\\google-cloud-sdk\\platform\\google_appengine\\google' which is my Google Cloud SDK and not the old App Engine SDK, which I no longer use. – Brian Kay Walker Oct 25 '18 at 17:21
  • Thankyou Brian Kay Walker. An infuriating few hours trying to find why my module didn't import. Found "google" in my site-packages, deleted it, but still didn't work. Who knew .pth files even existed?? – Alan Oct 24 '19 at 03:46
2

I had this same issue because I pip installed gcloud before downloading and installing the SDK. The pip install created a python package google which didn't contain the appengine submodule (which is found in the SDK folder). I uninstalled the gcloud and related packages. Then just pip installed the google-cloud-bigquery which is the only package I needed from gcloud. Everything works fine now.

Paul Bendevis
  • 2,381
  • 2
  • 31
  • 42
1

I faced similar error while calling Google Analytics API using AWS Lambda.

Workaround from (Schweigi1) helped me.

import googleapiclient
from googleapiclient.discovery_cache.base import Cache

class MemoryCache(Cache):
    _CACHE = {}

    def get(self, url):
        return MemoryCache._CACHE.get(url)

    def set(self, url, content):
        MemoryCache._CACHE[url] = content

Usage:

service = googleapiclient.discovery.build("analyticsreporting", "v4", http=http, credentials=credentials,cache=MemoryCache())

Hope this helps someone who is facing this issue in AWS Lambda.

Ash
  • 1,180
  • 3
  • 22
  • 36
1

First possible reason:

you don't install the python library in google cloud sdk, so you can run in cmd (as administrator):

gcloud components install app-engine-python.

Second possible reason:

your IDE is not success get into google libraries, they exist in:

C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine

or in:

C:\Users\[your user]\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine

You can see in attached link explain how to add these libraries to IDE external libraries: https://stackoverflow.com/a/24206781/8244338

Israel
  • 1,165
  • 11
  • 11
0

I got this error in python:

from google.appengine.api import search
ImportError: No module named appengine.api

I thought this would be something along the similar lines of what is happening in this thread.

So, my solution was to run "dev_appserver.py 'your yaml file' ". I got this solution following the below links:

1) https://cloud.google.com/appengine/docs/standard/python/tools/using-local-server

2) https://www.youtube.com/watch?v=BdqUY8lCuBI

Hope this helps!

Sanit
  • 1
-1

check if you named some file google.py :) in the same package, because this can shadow the import of google.appengine.ext. I had the same error:

python import error “No module named appengine.ext”

and deleteting the file solved the problem.

makkasi
  • 6,328
  • 4
  • 45
  • 60