Google App Engine: ImportError: No module named appengine.ext
Google App Engine: ImportError: No module named appengine.ext
我正在尝试为使用数据存储的 GAE 程序编写测试。
在 Google's Documentation 之后,我看到我应该将我的 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')
那么当文件是运行:
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
我已经在上面的位置安装了 SDK,然后在 /home/olly/google-cloud-sdk/platform/google_appengine/
中找到了 google
文件夹,里面有一个 __init__.py
,还有 appengine
.基本上,文件夹结构对我来说看起来不错,它们都被正确命名并且有 __init__.py
个文件。
在交互式控制台中,在 运行 执行上述命令后,我发现我可以 运行:
import google
没问题,但当我尝试时
import google.appengine
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named appengine
据我了解,目录中有 __init__.py()
个文件意味着它们可以按上述方式导入。
我还做了一个 sudo find / --name "google"
,唯一出现在我的 PYTHONPATH 中的是 /usr/local/lib/python2.7/dist-packages
,我明确删除了它,并且还在前面插入了我的其余路径。
我试过使用GAE自己的方法:
import dev_appserver
dev_appserver.fix_sys_path()
它为 sys.path 添加了很多路径,但仍然没有帮助我实现它。
我还发现,当我将 '/home/olly/Servers/google_appengine/google'
添加到我的路径时,我可以 运行:
import appengine.ext
但是运行宁:
from appengine.ext import ndb
原因:
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
我是不是漏掉了一些很明显的东西?我应该如何导入 ndb?
编辑:
我正在 运行 安装最新的 SDK (1.9.34),但我的 google_imports.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?')
另外,google.__path__
给了我 '/usr/local/lib/python2.7/dist-packages'
我认为我之前删除的路径。以下是我如何删除它的摘录:
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']
所以我的 sys.path 已更新,但 import google
似乎仍在从不再存在的路径导入,我想这将是我的问题的症结所在。我需要重新加载路径还是什么?
我 运行 经常 运行 在 virtualenv.
中解决这些问题
我同意 snakecharmerrb 你应该得到 print google.__file__
或 google.__path_
来弄清楚你正在导入什么。
此代码段也可能会解决您的问题:
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`
您使用的是哪个版本的 App Engine SDK?我正在使用最新的 SDK (1.9.34)。我在我的 ~/google_appengine/google/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?')
但是在您的堆栈跟踪中,在 google3.storage import 之后它似乎并没有进入 except 子句。
所以用最新的 SDK 试试同样的代码。
我正在尝试为使用数据存储的 GAE 程序编写测试。 在 Google's Documentation 之后,我看到我应该将我的 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')
那么当文件是运行:
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
我已经在上面的位置安装了 SDK,然后在 /home/olly/google-cloud-sdk/platform/google_appengine/
中找到了 google
文件夹,里面有一个 __init__.py
,还有 appengine
.基本上,文件夹结构对我来说看起来不错,它们都被正确命名并且有 __init__.py
个文件。
在交互式控制台中,在 运行 执行上述命令后,我发现我可以 运行:
import google
没问题,但当我尝试时
import google.appengine
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named appengine
据我了解,目录中有 __init__.py()
个文件意味着它们可以按上述方式导入。
我还做了一个 sudo find / --name "google"
,唯一出现在我的 PYTHONPATH 中的是 /usr/local/lib/python2.7/dist-packages
,我明确删除了它,并且还在前面插入了我的其余路径。
我试过使用GAE自己的方法:
import dev_appserver
dev_appserver.fix_sys_path()
它为 sys.path 添加了很多路径,但仍然没有帮助我实现它。
我还发现,当我将 '/home/olly/Servers/google_appengine/google'
添加到我的路径时,我可以 运行:
import appengine.ext
但是运行宁:
from appengine.ext import ndb
原因:
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
我是不是漏掉了一些很明显的东西?我应该如何导入 ndb?
编辑: 我正在 运行 安装最新的 SDK (1.9.34),但我的 google_imports.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?')
另外,google.__path__
给了我 '/usr/local/lib/python2.7/dist-packages'
我认为我之前删除的路径。以下是我如何删除它的摘录:
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']
所以我的 sys.path 已更新,但 import google
似乎仍在从不再存在的路径导入,我想这将是我的问题的症结所在。我需要重新加载路径还是什么?
我 运行 经常 运行 在 virtualenv.
中解决这些问题我同意 snakecharmerrb 你应该得到 print google.__file__
或 google.__path_
来弄清楚你正在导入什么。
此代码段也可能会解决您的问题:
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`
您使用的是哪个版本的 App Engine SDK?我正在使用最新的 SDK (1.9.34)。我在我的 ~/google_appengine/google/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?')
但是在您的堆栈跟踪中,在 google3.storage import 之后它似乎并没有进入 except 子句。
所以用最新的 SDK 试试同样的代码。