我正在尝试为使用数据存储的 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`
关于python - 谷歌应用引擎 : ImportError: No module named appengine. 分机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36022960/