我正在尝试为使用数据存储区的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
,我明确删除了它,并且还将其余的路径插入到前面。< / p>
我尝试使用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
似乎仍然从不再存在的路径导入,这将是我猜的问题的关键。我需要重新加载路径吗?
答案 0 :(得分:10)
总是在virtualenv内运行,我会更多地遇到这些问题。
我同意snakecharmerrb你应该打印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`
答案 1 :(得分:2)
您使用的是哪个版本的应用引擎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导入之后,它似乎没有进入except子句。
请尝试使用最新SDK的相同代码。