简体   繁体   English

在plpython3u过程中导入Python包时“找不到模块”

[英]“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). 我在MacOS (与标准Enterprise DB软件包一起安装)的postgres数据库上使用了plpython3存储的函数。

I can import standard python packages such as: 我可以导入标准的python包,例如:

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: 但是,我无法导入安装在我机器上常规python3目录中的安装包,该目录由brew定义:

$ which python3
/usr/local/bin/python3

So import foo would not work even though it would work in the regular python3 environment. 因此,即使在常规python3环境中也可以使用import foo也无法正常工作。

Would it be possible that the PostgreSQL server is not using the same python3 environment as me when running plpython3u ? 运行plpython3u时, PostgreSQL服务器是否可能未使用与我相同的python3环境? (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? (也许它使用的是python3解释器,这是MacOS等上的标准问题。)在发生这种情况时,我该如何检查以及如何更正配置?

And indeed, I created a stored function get_py that does the following: 确实,我创建了一个存储函数get_py ,它执行以下操作:

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 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 由Apple LLVM 6.0版编译的x86_64-apple-darwin上的PostgreSQL 11.5
  • I am not using any virtualenv here. 我在这里没有使用任何virtualenv

In addition to other environment variables like PGDATA in a shell script (pg_service.sh), set the PYTHONPATH='/path/to/python:/path/to/your/module' 除了其他环境变量(如Shell脚本(pg_service.sh)中的PGDATA )外,还要设置PYTHONPATH='/path/to/python:/path/to/your/module'

Eg cat /Users/postgres/pg_service.sh 例如cat /Users/postgres/pg_service.sh

export PGDATABASE=postgres
export PGUSER=postgres
export PGPORT=5432
export PATH=/Library/PostgreSQL/11/bin:$PATH
export PGLOCALEDIR=/Library/PostgreSQL/11/share/locale
export PYTHONUSERBASE=/Users/postgres/packaging_tutorial
export PYTHONPATH=/Library/edb/languagepack-11/Python-3.6:$PYTHONUSERBASE
pg_ctl -D /Library/PostgreSQL/11/data -l /Users/postgres/logfile $1

Start the server: 启动服务器:

> sudo -u postgres /Users/postgres/pg_service.sh start
waiting for server to start.... done
server started 

/Users/postgres/packaging_tutorial/example_pkg/__init__.py : /Users/postgres/packaging_tutorial/example_pkg/__init__.py

def retpy3():
    return 7/5

pg function: pg函数:

CREATE OR REPLACE FUNCTION expy3()
RETURNS text
LANGUAGE plpython3u
AS $$
import example_pkg
return example_pkg.retpy3()
$$;

Output 输出量

psql  -c 'select * from expy3()';
 expy3
--------
 1.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM