App Engine: ImportError:无名为appengine.ext的模块

时间:2022-04-11 23:15:59

I am trying to write a test for my GAE programme which uses the datastore. Following Google's Documentation, I see that I should be adding the path to my SDK into my PYTHONPATH. I did this using:

我正在为使用数据存储的GAE程序编写一个测试。根据谷歌的文档,我发现我应该将路径添加到我的SDK到我的PYTHONPATH中。我使用:

import sys
sys.path.remove('/usr/local/lib/python2.7/dist-packages')    # Has a 'google' module, which I want to be sure isn't interfering.
sys.path.insert(1,'/home/olly/google-cloud-sdk/platform/google_appengine')
sys.path.insert(1, '/home/olly/google-cloud-sdk/platform/google_appengine/lib/yaml/lib')

Then when the file is run:

当文件运行时:

Traceback (most recent call last):
 File "myapp_tests.py", line 20, in <module>
    from google.appengine.ext import ndb
ImportError: No module named appengine.ext

I have installed the SDK in the location above, and looking in /home/olly/google-cloud-sdk/platform/google_appengine/ I found the google folder, which has an __init__.py in it, along with appengine. Basically, the folder structure looks good to me, with them all being named correctly and having __init__.py files.

我在上面的位置安装了SDK,在/home/olly/google-cloud- SDK /platform/google_appengine/中找到了谷歌文件夹,它有一个__init__。里面有py和appengine。基本上,文件夹结构对我来说很好,它们都被正确命名并具有__init__。py文件。

In an interactive console, after running the commands above, I found that I could run:

在一个交互控制台中,运行了上面的命令后,我发现我可以运行:

import google

no problem, but when I tried

没问题,但是当我试的时候

import google.appengine
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named appengine

It was my understanding that having the __init__.py() files in the directories meant that they could be imported as above. I also did a sudo find / --name "google", and the only thing that showed up that is also in my PYTHONPATH was the /usr/local/lib/python2.7/dist-packages, which I explicitly removed, and also inserted the rest of my paths in front of anyway.

我的理解是,在目录中有__init__.py()文件意味着可以像上面那样导入它们。我还做了一个sudo find / -name“谷歌”,唯一出现在我的PYTHONPATH里的是/usr/local/lib/python2.7/dist-package,我显式地删除了它,并且在前面插入了我的其他路径。

I tried using GAE's own method of:

我尝试使用GAE自己的方法:

import dev_appserver
dev_appserver.fix_sys_path()

which added a whole lot of paths to sys.path, but still didn't help me make it work.

它为sys添加了很多路径。path,但仍然没有帮我让它起作用。

I also found that when I add '/home/olly/Servers/google_appengine/google' to my paths, I can run:

我还发现,当我在路径中添加'/home/olly/Servers/google_appengine/谷歌'时,我可以运行:

import appengine.ext

but running:

但运行:

from appengine.ext import ndb

causes:

原因:

Traceback (most recent call last):
  File "booking_function_tests.py", line 16, in <module>
     from appengine.ext import ndb
  File "/home/olly/Servers/google_appengine/google/appengine/ext/ndb/__init__.py", line 7, in <module>
     from tasklets import *
  File "/home/olly/Servers/google_appengine/google/appengine/ext/ndb/tasklets.py", line 69, in <module>
     from .google_imports import apiproxy_stub_map
  File "/home/olly/Servers/google_appengine/google/appengine/ext/ndb/google_imports.py"    , line 11, in <module>
     from google3.storage.onestore.v3 import entity_pb
ImportError: No module named google3.storage.onestore.v3

Am I missing something really obvious? How should I go about importing ndb?

我是不是漏掉了一些明显的东西?我该如何进口新开发银行?

EDIT: I'm running the latest SDK (1.9.34), but I have the following code in my google_imports.py:

编辑:我正在运行最新的SDK(1.9.34),但是在我的google_import. py中有以下代码:

try:
  from google.appengine.datastore import entity_pb
  normal_environment = True
except ImportError:
  try:
    from google3.storage.onestore.v3 import entity_pb
    normal_environment = False
  except ImportError:
    # If we are running locally but outside the context of App Engine.
    try:
      set_appengine_imports()
      from google.appengine.datastore import entity_pb
      normal_environment = True
    except ImportError:
      raise ImportError('Unable to find the App Engine SDK. '
                        'Did you remember to set the "GAE" environment '
                        'variable to be the path to the App Engine SDK?')

Also, google.__path__ gives me the '/usr/local/lib/python2.7/dist-packages' path which I thought I removed earlier. Here is an excerpt of how I'm removing it:

此外,谷歌。__path__为我提供了“/usr/local/lib/python2.7/dist-package”路径,我想我之前已经删除了。以下是我如何删除它的摘录:

import sys
sys.path.insert(1, '/home/olly/Servers/google_appengine')
sys.path.insert(1, '/home/olly/Servers/google_appengine/lib/yaml/lib')
sys.path.remove('/usr/local/lib/python2.7/dist-packages')

import google
print google.__path__
print sys.path


['/usr/local/lib/python2.7/dist-packages/google']
['/home/olly/Servers/google_appengine/myapp', '/home/olly/Servers/google_appengine/lib/yaml/lib', '/home/olly/Servers/google_appengine/google', '/home/olly/Servers/google_appengine', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']

So my sys.path is updated, but import google seems to still be importing from the no-longer-there path, which would be the crux of my problem I guess. Do I need to reload the path or something?

所以我的系统。path被更新了,但是导入谷歌似乎仍然是从不再存在的路径导入的,我想这是问题的关键所在。我需要重新加载路径还是别的什么?

2 个解决方案

#1


8  

I run into these problems a lot less by always running inside a virtualenv.

我经常在virtualenv中运行这些问题。

I agree with snakecharmerrb you should get print google.__file__ or google.__path_ to figure out exactly what you're importing.

我同意snakecharmerrb你应该打印谷歌。__file__或谷歌。确定您正在导入的是什么。

This snippet might also solve your problem:

这段代码也可以解决您的问题:

import google

gae_dir = google.__path__.append('/path/to/appengine_sdk//google_appengine/google')
sys.path.insert(0, gae_dir) # might not be necessary

import google.appengine # now it's on your import path`

#2


2  

Which version of app engine SDK are you using? I am using the latest SDK (1.9.34). I find the below snippet in my ~/google_appengine/google/appengine/ext/ndb/google_imports.py file

你正在使用哪个版本的app engine SDK ?我正在使用最新的SDK(1.9.34)。我在我的~/google_appengine/谷歌/appengine/ext/ndb/google_imports中找到下面的代码片段。py文件

try:
    from google3.storage.onestore.v3 import entity_pb
    normal_environment = False
except ImportError:
    # If we are running locally but outside the context of App Engine.
    try:
      set_appengine_imports()
      from google.appengine.datastore import entity_pb
      normal_environment = True
    except ImportError:
      raise ImportError('Unable to find the App Engine SDK. '
                    'Did you remember to set the "GAE" environment '
                    'variable to be the path to the App Engine SDK?')

But in your stack trace, after google3.storage import it doesn't seem to go inside the except clause.

但是在你的堆栈跟踪中,在google3之后。存储导入它似乎没有进入到except子句中。

So try the same code with latest SDK.

所以用最新的SDK尝试同样的代码。

#1


8  

I run into these problems a lot less by always running inside a virtualenv.

我经常在virtualenv中运行这些问题。

I agree with snakecharmerrb you should get print google.__file__ or google.__path_ to figure out exactly what you're importing.

我同意snakecharmerrb你应该打印谷歌。__file__或谷歌。确定您正在导入的是什么。

This snippet might also solve your problem:

这段代码也可以解决您的问题:

import google

gae_dir = google.__path__.append('/path/to/appengine_sdk//google_appengine/google')
sys.path.insert(0, gae_dir) # might not be necessary

import google.appengine # now it's on your import path`

#2


2  

Which version of app engine SDK are you using? I am using the latest SDK (1.9.34). I find the below snippet in my ~/google_appengine/google/appengine/ext/ndb/google_imports.py file

你正在使用哪个版本的app engine SDK ?我正在使用最新的SDK(1.9.34)。我在我的~/google_appengine/谷歌/appengine/ext/ndb/google_imports中找到下面的代码片段。py文件

try:
    from google3.storage.onestore.v3 import entity_pb
    normal_environment = False
except ImportError:
    # If we are running locally but outside the context of App Engine.
    try:
      set_appengine_imports()
      from google.appengine.datastore import entity_pb
      normal_environment = True
    except ImportError:
      raise ImportError('Unable to find the App Engine SDK. '
                    'Did you remember to set the "GAE" environment '
                    'variable to be the path to the App Engine SDK?')

But in your stack trace, after google3.storage import it doesn't seem to go inside the except clause.

但是在你的堆栈跟踪中,在google3之后。存储导入它似乎没有进入到except子句中。

So try the same code with latest SDK.

所以用最新的SDK尝试同样的代码。