Обсуждение: pl/pyton: exceptions.ImportError: No module named email.Parser
Hi,
I need to parse e-mail messages in pl/python functions. So, I write:
dbmail=# create or replace function get_header(varchar) returns varchar
as $$
dbmail$# import email.Parser
dbmail$# parser = email.Parser.Parser()
dbmail$# message = parser.parse(args[0])
dbmail$# return message.get("From")
dbmail$# $$ language plpythonu;
CREATE FUNCTION
dbmail=#
dbmail=# select get_header(messageblk) from dbmail_messageblks;
ERROR: plpython: function "get_header" failed
DETAIL: exceptions.ImportError: No module named email.Parser
Simple pl/python functions works fine:
dbmail=# create or replace function test(varchar) returns varchar as $$
dbmail$# return args[0]
dbmail$# $$ language plpythonu;
CREATE FUNCTION
dbmail=#
dbmail=# select test('test'); test
------ test
(1 record)
Message parsing with standalone python works too:
$ cat mail.py
import sys
import email.Parser
parser = email.Parser.Parser()
message = parser.parse(sys.stdin)
print message.get("From")
$ python mail.py < mbox
test@mydomain.com
Why can't I use "import email.Parser" in pl/python function?
--
Thanks,
Eugene Prokopiev
On Sun, Nov 27, 2005 at 06:17:06PM +0300, Eugene Prokopiev wrote: > dbmail=# select get_header(messageblk) from dbmail_messageblks; > ERROR: plpython: function "get_header" failed > DETAIL: exceptions.ImportError: No module named email.Parser Your function works here with Python 2.4.2 and PostgreSQL 8.0.4 and 8.1.0, although I had to use parser.parsestr() instead of parser.parse() because the latter expects a file-like argument (an alternative would be to use parse() and pass a StringIO object). As for why the import fails, might PL/Python be using a different version of Python than the command-line python program? What does the following function show? CREATE FUNCTION pyversion() RETURNS text AS $$ import sys return sys.version + '\n' + '\n'.join(sys.path) $$ LANGUAGE plpythonu; Does this function show the same version and module search path as an ordinary Python program? python -c 'import sys; print sys.version, sys.path' What do the following commands show? ldd /path/to/python ldd /path/to/plpython.so -- Michael Fuhr
I use PostgreSQL 8.1.0 and Python 2.3.3 $ psql -U postgres test test=# CREATE FUNCTION pyversion() RETURNS text AS $$ test$# import sys test$# return sys.version + '\n' + '\n'.join(sys.path) test$# $$ LANGUAGE plpythonu; CREATE FUNCTION test=# select pyversion(); pyversion ------------------------------------------------------ 2.3.3 (#1, Jul 5 2004, 13:54:49) [GCC 3.3.3 20040412 (ALT Linux, build 3.3.3-alt5)] /usr/lib/python23.zip /usr/lib/python2.3/ /usr/lib/python2.3/plat-linux2 /usr/lib/python2.3/lib-tk /usr/lib/lib-dynload (1 record) test=# \q $ python -c 'import sys; print sys.version, sys.path' 2.3.3 (#1, Jul 5 2004, 13:54:49) [GCC 3.3.3 20040412 (ALT Linux, build 3.3.3-alt5)] ['', '/usr/lib/python23.zip', '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2', '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload', '/usr/lib/python2.3/site-packages'] $ ldd /usr/bin/python libpython2.3.so.1.0 => /usr/lib/libpython2.3.so.1.0 (0x00116000) libpthread.so.0 => /lib/libpthread.so.0(0x001fb000) libdl.so.2 => /lib/libdl.so.2 (0x0024f000) libutil.so.1 => /lib/libutil.so.1(0x00253000) libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x00257000) libm.so.6 => /lib/libm.so.6(0x00310000) libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00333000) libc.so.6 => /lib/libc.so.6 (0x0033c000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000) $ ldd /usr/lib/pgsql/plpython.so libpython2.3.so.1.0 => /usr/lib/libpython2.3.so.1.0 (0x0011f000) libpthread.so.0=> /lib/libpthread.so.0 (0x00204000) libdl.so.2 => /lib/libdl.so.2 (0x00258000) libutil.so.1=> /lib/libutil.so.1 (0x0025c000) libm.so.6 => /lib/libm.so.6 (0x00260000) libc.so.6 => /lib/libc.so.6(0x00283000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000) Python versions are the same -- Thanks, Eugene Prokopiev
[Forwarding this message to the list so the resolution gets into the archives.] On Mon, Nov 28, 2005 at 09:00:56PM +0300, Eugene Prokopiev wrote: > The problem with pl/python was in my configuration. My Linux (ALT Linux) > install PostgreSQL into chroot by default, so I need to copy some python > libs into chroot too. -- Michael Fuhr