[英]Why can't I import a model from a file containing import of any of the Google App Engine components?
I'm attempting to build a simple Google App Engine App using TDD. 我正在尝试使用TDD构建一个简单的Google App Engine应用程序。
When I attempt to import my Customer model I get an error: 当我尝试导入客户模型时,出现错误:
$ python functional_tests.py
Traceback (most recent call last):
File "functional_tests.py", line 4, in <module>
from models import Customer
File "/Users/Bryan/work/GoogleAppEngine/dermalfillersecrets/models.py", line 1, in <module>
from google.appengine.ext import ndb
ImportError: No module named google.appengine.ext
This is the code in model.py: 这是model.py中的代码:
from google.appengine.ext import ndb
class Customer(ndb.Model):
email = ndb.StringProperty()
name = ndb.StringProperty(indexed=True)
This is the code from functional_tests.py: 这是来自functional_tests.py的代码:
from selenium import webdriver
import unittest
from models import Customer
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def test_quest_can_submit_contact_info(self):
self.browser.get('http://localhost:9080')
self.browser.find_element_by_name('id_name').send_keys("Kallie Wheelock")
self.browser.find_element_by_name('id_email').send_keys("kallie@gmail.com")
self.browser.find_element_by_name('submit').submit()
Customer.query(Customer.name("Kallie Wheelock")).delete()
Your specific problem is that you are not importing the app engine SDK to your sys path. 您的特定问题是您没有将App Engine SDK导入到sys路径。
sys.path.append("/usr/local/google_appengine")
solves the ImportError. sys.path.append("/usr/local/google_appengine")
解决了ImportError。
But to solve your TDD challenge completely you have to use dev_appserver and testbed together with selenium. 但是要完全解决您的TDD挑战,您必须使用dev_appserver并与硒一起使用测试平台。 I took the solution proposed in https://stackoverflow.com/a/20986246/710851 and configured for your use case.
我采用了https://stackoverflow.com/a/20986246/710851中提出的解决方案,并针对您的用例进行了配置。
import sys
sys.path.append("/usr/local/google_appengine")
sys.path.append("/usr/local/google_appengine/lib/yaml/lib")
sys.path.append("/usr/local/google_appengine/lib/webapp2-2.5.2")
sys.path.append("/usr/local/google_appengine/lib/django-1.5")
sys.path.append("/usr/local/google_appengine/lib/cherrypy")
sys.path.append("/usr/local/google_appengine/lib/concurrent")
sys.path.append("/usr/local/google_appengine/lib/docker")
sys.path.append("/usr/local/google_appengine/lib/requests")
sys.path.append("/usr/local/google_appengine/lib/websocket")
sys.path.append("/usr/local/google_appengine/lib/fancy_urllib")
sys.path.append("/usr/local/google_appengine/lib/antlr3")
def setUp(self):
# setup dev_appserver
APP_CONFIGS = ['app.yaml']
python_runtime._RUNTIME_ARGS = [
sys.executable,
os.path.join(os.path.dirname(dev_appserver.__file__),
'_python_runtime.py')
]
options = devappserver2.PARSER.parse_args([
'--admin_port', '0',
'--port', '9080',
'--datastore_path', ':memory:',
'--logs_path', ':memory:',
'--skip_sdk_update_check',
'--',
] + APP_CONFIGS)
server = devappserver2.DevelopmentServer()
server.start(options)
self.server = server
# setup selenium
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
# setup the testbed
self.tb = testbed.Testbed()
self.tb.setup_env(current_version_id='testbed.version')
self.tb.activate()
self.tb.init_user_stub()
self.tb.init_datastore_v3_stub()
self.tb.init_memcache_stub()
def tearDown(self):
self.browser.quit()
self.tb.deactivate()
self.server.stop()
import os
import sys
sys.path.append("/usr/local/google_appengine")
sys.path.append("/usr/local/google_appengine/lib/yaml/lib")
sys.path.append("/usr/local/google_appengine/lib/webapp2-2.5.2")
sys.path.append("/usr/local/google_appengine/lib/django-1.5")
sys.path.append("/usr/local/google_appengine/lib/cherrypy")
sys.path.append("/usr/local/google_appengine/lib/concurrent")
sys.path.append("/usr/local/google_appengine/lib/docker")
sys.path.append("/usr/local/google_appengine/lib/requests")
sys.path.append("/usr/local/google_appengine/lib/websocket")
sys.path.append("/usr/local/google_appengine/lib/fancy_urllib")
sys.path.append("/usr/local/google_appengine/lib/antlr3")
import dev_appserver
from google.appengine.tools.devappserver2 import devappserver2
from google.appengine.tools.devappserver2 import python_runtime
from google.appengine.ext import testbed
from selenium import webdriver
import unittest
from models import Customer
class NewVisitorTest(unittest.TestCase):
def setUp(self):
# setup the dev_appserver
APP_CONFIGS = ['app.yaml']
python_runtime._RUNTIME_ARGS = [
sys.executable,
os.path.join(os.path.dirname(dev_appserver.__file__),
'_python_runtime.py')
]
options = devappserver2.PARSER.parse_args([
'--admin_port', '0',
'--port', '9080',
'--datastore_path', ':memory:',
'--logs_path', ':memory:',
'--skip_sdk_update_check',
'--',
] + APP_CONFIGS)
server = devappserver2.DevelopmentServer()
server.start(options)
self.server = server
# setup selenium
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
# setup the testbed
self.tb = testbed.Testbed()
self.tb.setup_env(current_version_id='testbed.version')
self.tb.activate()
self.tb.init_user_stub()
self.tb.init_datastore_v3_stub()
self.tb.init_memcache_stub()
def tearDown(self):
self.browser.quit()
self.tb.deactivate()
self.server.stop()
def test_quest_can_submit_contact_info(self):
self.browser.get('http://localhost:9080')
self.browser.find_element_by_name('id_name').send_keys("Kallie Wheelock")
self.browser.find_element_by_name('id_email').send_keys("kallie@gmail.com")
self.browser.find_element_by_name('submit').submit()
Customer.query(Customer.name("Kallie Wheelock")).delete()
In the latest version make sure to add this line 在最新版本中,请确保添加此行
if 'google' in sys.modules:
del sys.modules['google']
Full Example below 下面的完整示例
import sys
sys.path.append('/usr/local/google_appengine/')
sys.path.append('/usr/local/google_appengine/lib/yaml/lib/')
if 'google' in sys.modules:
del sys.modules['google']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.