Interacting with a Zope database from Python |
Created by karl . Last modified 2003-10-23 13:45:24. |
How to explore a Zope database from a Python prompt. |
I always forget how to spell things. Here's how I connect to a Zope database from a Python prompt. The Zope instance must be stopped first. karl@iwana:~$ python Python 2.1.3 (#1, Sep 7 2002, 15:29:56) [GCC 2.95.4 20011002 (Debian prerelease)] on linux2 Type "copyright", "credits" or "license" for more information. >>> import os >>> os.environ['INSTANCE_HOME'] = '/export/play/karl/working/dev/dotorg/zope_instance' >>> os.environ['SOFTWARE_HOME'] = '/usr/local/lib/zope-2.6.2/lib/python' >>> import sys >>> sys.path.insert(0, '/usr/local/lib/zope-2.6.2/lib/python') >>> import ZPublisher >>> import Zope >>> app = Zope.app()Now app is the root object of the Zope instance. If running ZEO, the Zope instance doesn't have to be stopped first, and the invocation is: os.environ['INSTANCE_HOME'] = '/u1/Portal/Devel/ZapCentral' # path to the top of the instance os.chdir('/u1/Portal/Devel/Zope-2_2-branch/lib/python') # lib/python dir in the server tree os.environ['ZEO_SERVER_PORT'] = '5802' import ZServer import Zope app = Zope.app() Another alternative is the medusa monitor. The server doesn't have to be stopped when using the monitor without ZEO. You must be running ZServer with the monitor server (see Z2.py for the proper invocation), be accessing the monitor on the same host as the server, and have a non-encrypted superuser password. karl@iwana:/usr/local/lib/zope-2.6$ cd ZServer/medusa karl@iwana:/usr/local/lib/zope-2.6/ZServer/medusa$ python monitor_client.py localhost 9999 /usr/lib/python2.1/regsub.py:15: DeprecationWarning: the regsub module is deprecated; please use re.sub() DeprecationWarning) Enter Password: warning: unhandled connect event Python 2.1.3 (#1, Sep 7 2002, 15:29:56) [GCC 2.95.4 20011002 (Debian prerelease)] Copyright (c) 2001-2002 Python Software Foundation. All Rights Reserved. Copyright (c) 2000 BeOpen.com. All Rights Reserved. Copyright (c) 1995-2001 Corporation for National Research Initiatives. All Rights Reserved. Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam. All Rights Reserved. Welcome to However, every action that you perform will show up on the server console, which may obscure messages that you want to pay attention to. Also, some useful results such as AttributeErrors raised by expressions entered into the monitor are only displayed on the console, so you have to keep an eye on it. I assume that this is because the monitor actually attaches to the ZServer process, and protects the server with a try block. Because of this, the monitor is useful for debugging some resource issues that can't be seen by mounting the database. See also these debugging bookmarks. |