[Zope-DB] Using psycopg directly in Zope Python script

Charlie Clark charlie at egenix.com
Sat Jul 2 08:01:14 EDT 2005

On 2005-07-01 at 23:28:51 [+0200], Jørgen Frøjk Kjærsgaard 
<jfk at metation.com> wrote:
> I need to be able to access a Postgresql database directly from Zope
> Python scripts (Z SQL Methods is not an option as the SQL code occurs in
> modules that must be re-usable in a non-Zope context).
> After several attempts at getting the DB-API connection from a Psycopg
> connection object created in Zope, I tried this in a Zope Python script:
> import psycopg
> dbcon = psycopg.connect('host=%s dbname=%s user=%s' % ('dbhost',
> 'dbname', 'username'))
> When run outside Zope, it opens a connection as expected. When run in
> Zope, however, I get "thrown out" - the browser asks me to enter my user
> name and password again. I get no error message!
> Does Zope somehow create a restricted environment where opening a
> database connection is impossible? Is there a way to bypass that
> restriction?

Yes, of course it does: it's an application server and the whole point of 
database adapters is to make your life easier! You should read the 
documentation about PythonScripts

If you need to do some stuff with the database that isn't directly possible 
in a ZSQL method then you might want to call the DA from within a 
PythonScript or ExternalMethod as Andreas has suggested. Some DA's such as 
our mxODBCZopeDA expose additional functionality for use in 
ExternalMethods. If you want to work directly with the database outside of 
Zope then you will have to use an ExternalMethod but note that this will 
put you outside of Zope's transaction mechanism which is *generally* very 

Charlie Clark

Professional Python Services directly from the Source
 >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::

More information about the Zope-DB mailing list