Tuesday 10 September 2019

"Module not found" when importing a Python package within a plpython3u procedure

I am using a plpython3 stored function, on a postgres database on MacOS (installed with standard Enterprise DB package).

I can import standard python packages such as:

CREATE OR REPLACE FUNCTION foo(x double precision)
RETURNS double precision
LANGUAGE plpython3u
AS $$
import math
...
$$

I cannot, however import packages I have installed on the regular python3 directory on my machine, which is defined by brew:

$ which python3
/usr/local/bin/python3

So import foo would not work even though it would work in the regular python3 environment.

Would it be possible that the postgresql server is not using the same python3 environment as me, when running plpython3u? (perhaps it is using the python3 interpreter which is standard issue on MacOS etc.) How can I check on that and how could I correct the configuration, in the event?

And indeed, I created a stored function get_py that does the following:

import os
return os.popen('which python').read()

And it returned:

> select get_py()
+-----------------+
| get_py          |
|-----------------|
| /usr/bin/python |
+-----------------+

(and nothing for which python3). Which seems to demonstrate that it is not using the interpreter I want!

How do I change this?

Config info

  • PostgreSQL 11.5 on x86_64-apple-darwin, compiled by Apple LLVM version 6.0
  • I am not using any virtualenv here.


from "Module not found" when importing a Python package within a plpython3u procedure

No comments:

Post a Comment