NDB in a python/ipython shell

826 views
Skip to first unread message

Robellard, Michael

unread,
Jan 22, 2012, 11:40:56 AM1/22/12
to appengine-...@googlegroups.com
I looked through the Google Group, the readme and the docs and
couldn't find an answer to this question.

How do you set up a shell to run NDB. Everything imports OK, but when
I run for instance a get from a key I get:

AssertionError: No api proxy found for service "memcache"

I have my dev appserver running, I am guessing, there is some setup
piece I need to do. Probably something similar to the work done at the
top of the unit test harness.

--
Michael Robellard
(216)288-2745
Play Railroad Empire: http://www.therailroadempire.com

Guido van Rossum

unread,
Jan 22, 2012, 11:54:38 AM1/22/12
to appengine-...@googlegroups.com
Have a look at some of the setup code in the file startup.py in the
repo and zip file (which exists to support "make python" which is how
I use NDB interactively). I think this is what you need:

import os

from google.appengine.api import apiproxy_stub_map
from google.appengine.api import datastore_file_stub
from google.appengine.api import memcache
from google.appengine.api.memcache import memcache_stub
from google.appengine.api import taskqueue
from google.appengine.api.taskqueue import taskqueue_stub
from google.appengine.api import urlfetch_stub

apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
ds_stub = datastore_file_stub.DatastoreFileStub('_', None)
apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', ds_stub)
mc_stub = memcache_stub.MemcacheServiceStub()
apiproxy_stub_map.apiproxy.RegisterStub('memcache', mc_stub)
tq_stub = taskqueue_stub.TaskQueueServiceStub()
apiproxy_stub_map.apiproxy.RegisterStub('taskqueue', tq_stub)
uf_stub = urlfetch_stub.URLFetchServiceStub()
apiproxy_stub_map.apiproxy.RegisterStub('urlfetch', uf_stub)
os.environ['APPLICATION_ID'] = '_'

If you want the datastore contents to persist, you'll have to pass a
file to the DatastoreFileStub constructor. If you want to read/write
the data written by your app (running in dev_appserver) you have to
make sure the filename matches the --datastore_path argument to
dev_appserver.py, and you have to make the appid match app.yaml (the
above code uses '_' in a few places).

Finally, you can also do it with remote_api, but that's a different
setup -- you probably have to read the docs for remote_api. That way
it would access the real datastore associated with your app. (But
beware that the assumptions NDB makes about Context lifetime might be
incorrect in that case.)

--Guido

--
--Guido van Rossum (python.org/~guido)

Robellard, Michael

unread,
Jan 22, 2012, 1:39:07 PM1/22/12
to appengine-...@googlegroups.com
Thatnks Guido,

This is exactly what I need.

Keith Fitzgerald

unread,
May 30, 2013, 9:22:12 AM5/30/13
to appengine-...@googlegroups.com
is there a way to use taskqueue interactively as well?

Guido van Rossum

unread,
May 30, 2013, 10:41:05 AM5/30/13
to appengine-...@googlegroups.com
I believe not, it interacts with an active service that implements the
task queue behavior. This service is only running if you run the dev
appserver. You can use the shell interaction window of the dev
appserver to interact with it however.

--Guido
> --
> You received this message because you are subscribed to the Google Groups
> "appengine-ndb-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to appengine-ndb-di...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages