分拆:
我已经尝试了在plpython3u过程中导入Python包时“未找到模块”的想法,我不知道如何使用被接受的答案使它在Linux上运行。通常情况下,这里应该是重复的,但是如果在使用的路径上与MacOS有区别,那么可能会有一个新的问题。
安装PostgreSQL 13
我在WSL2 (Ubuntu20.04)。我安装了官方PostgreSQL下载页面的命令,这些命令来自Ubuntu的PostgreSQL Apt库。
# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package lists:
sudo apt-get update
# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql
这给了我PostgreSQL 13。
安装正确的plpython3u
我用来自plpython3u的命令安装了PostgreSQL:如何安装plpythonu扩展
sudo apt-cache search ".*plpython3.*"
sudo apt-get install postgresql-contrib postgresql-plpython3-13
因为我在WSL,所以我开始这项服务时:
service postgresql start
更改角色并运行psql:
sudo su postgres
psql
在没有导入包的情况下安装和测试PostgreSQL
在postgreSQL中直接将Python版本作为存储过程检查:
CREATE OR REPLACE FUNCTION return_version()
RETURNS VARCHAR
AS $$
import sys
return sys.version
$$ LANGUAGE plpython3u;
输出:
CREATE FUNCTION
测试:
postgres=# SELECT return_version();
return_version
------------------------------------------
3.8.10 (default, Jun 2 2021, 10:49:15) +
[GCC 9.4.0]
(1 row)
显示对于"PostgreSQL 13",将安装“Python3.8.10”。
这正是在Linux上可以找到的。如果我只是输入一个我没有的Python版本,比如3.7
python3.7
我得到了概述:
Command 'python3.7' not found, did you mean:
command 'python3.8' from deb python3.8 (3.8.10-0ubuntu1~20.04)
command 'python3.9' from deb python3.9 (3.9.5-3~20.04.1)
command 'python2.7' from deb python2.7 (2.7.18-1~20.04.1)
确认没有安装3.8
的两个并行安装,很明显,Linux 3.8.10
是plpython3u在PostgreSQL中使用的版本。
用导入的包进行plpython3u存储过程测试
我尝试了一个从简略地说是在PostgreSQL中的意思中导入包的函数
CREATE OR replace FUNCTION kmeans(input_table text, columns text[], clus_num int) RETURNS bytea AS
$$
from pandas import DataFrame
from sklearn.cluster import KMeans
from pickle import dumps
all_columns = ",".join(columns)
if all_columns == "":
all_columns = "*"
rv = plpy.execute('SELECT %s FROM %s;' % (all_columns, plpy.quote_ident(input_table)))
frame = []
for i in rv:
frame.append(i)
df = DataFrame(frame).astype('float')
kmeans = KMeans(n_clusters=clus_num, random_state=0).fit(df._get_numeric_data())
return dumps(kmeans)
$$ LANGUAGE plpython3u;
输出:
CREATE FUNCTION
测试抛出错误:
postgres=# SELECT kmeans('my_table', ARRAY['col1', 'col2'],3);
ERROR: ModuleNotFoundError: No module named 'pandas'
CONTEXT: Traceback (most recent call last):
PL/Python function "kmeans", line 3, in <module>
from pandas import DataFrame
PL/Python function "kmeans"
然后,我在Python 3.8
中安装了熊猫
python3.8 -m pip install pandas
Collecting pandas
Downloading pandas-1.3.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.5 MB)
|████████████████████████████████| 11.5 MB 6.0 MB/s
Collecting python-dateutil>=2.7.3
Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
|████████████████████████████████| 247 kB 4.7 MB/s
Collecting numpy>=1.17.3
Downloading numpy-1.21.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.8 MB)
|████████████████████████████████| 15.8 MB 131 kB/s
Collecting pytz>=2017.3
Downloading pytz-2021.1-py2.py3-none-any.whl (510 kB)
|████████████████████████████████| 510 kB 5.7 MB/s
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.7.3->pandas) (1.14.0)
Installing collected packages: python-dateutil, numpy, pytz, pandas
WARNING: The scripts f2py, f2py3 and f2py3.8 are installed in '/home/my_user/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.21.2 pandas-1.3.2 python-dateutil-2.8.2 pytz-2021.1
熊猫现在被储存在:
python3.8 -m pip show pandas
Name: pandas
Version: 1.3.2
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author: The Pandas Development Team
Author-email: pandas-dev@python.org
License: BSD-3-Clause
Location: /home/my_user/.local/lib/python3.8/site-packages
Requires: numpy, python-dateutil, pytz
Required-by:
但是测试仍然抛出相同的错误:
postgres=# SELECT kmeans('my_table', ARRAY['col1', 'col2'],3);
ERROR: ModuleNotFoundError: No module named 'pandas'
CONTEXT: Traceback (most recent call last):
PL/Python function "kmeans", line 3, in <module>
from pandas import DataFrame
PL/Python function "kmeans"
问题
如何安装熊猫(或我需要的任何其他软件包),以便在Linux上的PostgreSQL 13 plpython3u扩展找到它?
PS:可能不需要附带说明
可能不需要:在plpython3u存储过程中使用“哪个”来检查Python
附带的问题中还有另一个版本测试,但在扩展中没有显示任何内容,命令与普通bash中的命令完全相同。因此,将下面的值简化为对plpython3u函数的随机测试。我加这个只是因为你永远不应该百分之百肯定。对我来说,很明显,这并没有说明plpython3u实际使用的Python版本。
CREATE OR REPLACE FUNCTION get_py()
RETURNS VARCHAR
AS $$
import os
return os.popen('which python3').read()
$$ LANGUAGE plpython3u;
输出:
CREATE FUNCTION
测试:
SELECT get_py();
get_py
------------------
/usr/bin/python3+
(1 row)
'which python3.8'
也是如此
select get_py();
get_py
--------------------
/usr/bin/python3.8+
(1 row)
可能不需要:使用make和默认Python版本来安装
也许在安装之前必须更改Python的默认版本?这只是来自使用make的安装,Python设置在/etc/make.conf中的一个非常模糊的猜测,下面是使用的代码(2018年的问题):
将/etc/make.conf
更改为
DEFAULT_VERSIONS+= python=3.6
据说这通过并安装了plpython3u (我还没有对此进行测试):
root@db% cd /usr/ports/databases/posgresql10-plpython
root@db% make fetch
root@db% make extract
root@db% cd /usr/ports/databases/postgresql10-plpython/work/postgresql-10.5
root@db% ./configure PYTHON=/usr/local/bin/python3 --with-python --with-libraries=/usr/local/lib --with-includes=/usr/local/include/
root@db% cd /usr/ports/databases/posgresql10-plpython
root@db% make install
发布于 2021-09-01 18:24:17
问题是:
python3.8 -m pip install pandas
在运行命令的用户的主目录中向site-packages
安装一个包(本例中为熊猫),如下所示:
/home/my_user/.local/lib/python3.8/site-packages
在Postgres中运行的plpython3u
扩展正在寻找系统范围内的site-packages
中的包。要将包放在那里,您需要做以下工作:
sudo python3.8 -m pip install pandas
https://stackoverflow.com/questions/69013111
复制相似问题