问题
I need to import a 3rd party module inside my pl/python function. It seems pl/python uses an internal python that does not have any 3rd party modules.
I get this kind of error:
ERROR: PL/Python: PL/Python function "to_tsvector_luc" failed
DETAIL: <type 'exceptions.ImportError'>: No module named lucene
********** Error **********
ERROR: PL/Python: PL/Python function "to_tsvector_luc" failed
SQL state: XX000
Detail: <type 'exceptions.ImportError'>: No module named lucene
How do I install the module into pl/python, so that I can import it from inside my stored procedure code?
回答1:
pl/python has access to the all the modules that the normal Python interpreter would have as long as they are in the $PYTHONPATH
on the server (and the user that runs the postgres service). Does import lucene
work if you run it in the Python interpreter on the server?
If your module is installed somewhere else (e.g. not dist-packages etc.), then you would need to edit your /etc/postgresql/9.1/main/environment
(adjust to your PostgreSQL version) file on the server and add something like PYTHONPATH='<path to your module>'
.
回答2:
Since modifying PYTHONPATH of postgres user will likely need a server restart, it's somewhat easier to add the path from within Python, via
from sys import path
path.append( '/path/to/your/module' )
回答3:
For me, it was about knowing which version Python Postgres was looking at and then installing to the /local/lib directory and NOT .local directory which is not recognised in Postgres.
In Postgres, created this function to identify the Python version it was using: .
CREATE OR REPLACE FUNCTION python_version()
RETURNS pg_catalog.text AS $BODY$
import sys
plpy.info(sys.version)
return 'finish'
BODY$
LANGUAGE plpython3u VOLATILE SECURITY DEFINER
Execute function using the following:
select python_version()
In Python:
import sys
sys.version
Both the script and Python said: INFO: 3.7.3 (default, Aug 20 2019, 17:04:43)
pip3 install placed the library into this directory: /home/username/.local/lib/python3.7/site-packages
To make the package available to ALL users (including Postgres). I used umask:
sudo su
cd ~
umask 022
pip3 install <package name>
NOTE: The package installs in: /usr/local/lib# cd python3.7/dist-packages NOT in /usr/lib/python3/dist-packages#
来源:https://stackoverflow.com/questions/8032293/how-to-install-3rd-party-module-for-postgres-pl-python