Re: psycopg used in a ASP page fails

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: Jason Erickson <jerickso(at)stickpeople(dot)com>
Cc: Barend Köbben <kobben(at)itc(dot)nl>, "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
Subject: Re: psycopg used in a ASP page fails
Date: 2011-02-17 09:46:12
Message-ID: AANLkTim6p8vp0+h48-sC1Cu1govjQqi+WKsC_QnWdrwE@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On Wed, Feb 16, 2011 at 8:37 PM, Jason Erickson
<jerickso(at)stickpeople(dot)com> wrote:

> Instead of 'import psycopg2', does the following code work when run as an
> ASP script?:
> ---------------------------------------------------------- from
> psycopg2._psycopg import __version__
> ----------------------------------------------------------
>
> For craziness sakes, does the following code work in ASP land?:
> ---------------------------------------------------------- from psycopg2
> import tz ----------------------------------------------------------
>
>
> Also, verify that the following file exist (which it should, since it works
> other ways):
>        %PYTHON_INSTALL_DIR%\Lib\site-packages\psycopg2\tz.py
>
> Also, can you verify that there is not a psycopg directory or psycopg.py
> file in that same directory?
>        %PYTHON_INSTALL_DIR%\Lib\site-packages\psycopg2
>
>
> You didn't mention what version of python you are using?
>
>
> With an ASP script, can you also try this code (I'm assuming that
> Response.Write is similar to print, change otherwise):
> ----------------------------------------------------------
> import sys Reponse.Write(sys.path)
> ----------------------------------------------------------
> This code will print the 'search' path for python modules.  This search path
> should be the same as when you run the python interpreter by hand, tho maybe
> ASP is adding something to it??  Verify that they are the same.
>
>
> Can you also verify that it is finding the correct 'module' with the
> following asp code:
> ----------------------------------------------------------
> import imp
> f, filename, description = imp.find_module('psycopg2')
> Response.Write('psycopg2 file location: '+filename)
> ----------------------------------------------------------
> What it prints should be the path to the psycopg2 install directory. Verify
> that it is pointing to the correct directory where you installed psycopg2,
> and that the file tz.py is in that directory.
>
>
>
> This is interesting because of where it fails.  First off, it is finding a
> psycopg2 directory, as it is importing (or trying to import) __init__.
> Second, it is not failing on loading the dll, but failing on a step before
> loading the dll, where it is loading a module inside the psycopg2 package
> (tz).  It is almost as if the sys.path is different between ASP land and
> normal land, causing the issue.
>
> It is possible that it is similar to the manifest issue that we see with
> modwsgi, but I'm thinking not.  Please try the beta version at:
>        http://www.stickpeople.com/projects/python/win-psycopg/devel/
> as well.

Hi,

I've just found how to reproduce the the "cannot find tz". I think
it's still related to the failed dll import, but it happens _on the
second import_ of psycopg.

Artificially making the _psycopg import failing:

$ sudo mv -vi /usr/lib/{,-}libpq.so.5

The first import gives a clean error message:

>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/piro/dev/psycopg2/dist/tmp/psycopg2/__init__.py",
line 69, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: libpq.so.5: cannot open shared object file: No such
file or directory

But the second gives:

>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/piro/dev/psycopg2/dist/tmp/psycopg2/__init__.py",
line 65, in <module>
from psycopg2 import tz
ImportError: cannot import name tz

So probably the first traceback is swallowed away somewhere.

The reason is in the order of the imports in psycopg2/__init__.py:
after the first import error what is imported in the system is:

>>> print "\n".join(filter(re.compile("psyco").search,
sorted(sys.modules.keys())))
psycopg2.datetime
psycopg2.decimal
psycopg2.psycopg2
psycopg2.sys
psycopg2.time
psycopg2.tz
psycopg2.warnings

it seems an inconsistent status with "psycopg2.tz" was loaded but
"psycopg2" is not available. Taking the care to have _psycopg imported
before the other submodules:

--- __init__.py 2011-02-16 23:02:27.000000000 +0000
+++ __init__.py.new 2011-02-17 09:35:41.752056278 +0000
@@ -62,8 +62,6 @@
RuntimeWarning)
del sys, warnings

-from psycopg2 import tz
-
# Import the DBAPI-2.0 stuff into top-level module.

from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
@@ -78,6 +76,8 @@
from psycopg2._psycopg import connect, apilevel, threadsafety, paramstyle
from psycopg2._psycopg import __version__

+from psycopg2 import tz
+
# Register default adapters.

import psycopg2.extensions as _ext

we can get the consistent import error about the failed _psycopg import.

Barend, if you could change the psycopg2/__init__.py as shown above
and repeat your test we may get the real reason of the failed import.
Please don't forget to include the Python and psycopg versions you are
using, and to make a test with the 2.4 beta2 too. Thank you.

-- Daniele

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Barend Köbben 2011-02-17 10:05:52 Re: psycopg used in a ASP page fails
Previous Message Jason Erickson 2011-02-16 21:00:46 Re: RELEASE: psycopg 2.4 beta 2