Thread: ERROR: ImportError: No module named 'psutil'
Hi Everyone,
Environment
OS : Linux ubuntu 4.4.0-87-generic
Database: PostgreSQL 10.7 on x86_64-pc-linux-gnu,
Extension: plpython3u
Python version: Python 3.5.2
I am trying to run a python function using the language plpython3u and I am getting the below error, though I have installed psutil.
postgres=# select * from get_psutil_mem();
ERROR: ImportError: No module named 'psutil'
CONTEXT: Traceback (most recent call last):
PL/Python function "get_psutil_mem", line 2, in <module>
from psutil import virtual_memory, swap_memory
PL/Python function "get_psutil_mem"
ERROR: ImportError: No module named 'psutil'
CONTEXT: Traceback (most recent call last):
PL/Python function "get_psutil_mem", line 2, in <module>
from psutil import virtual_memory, swap_memory
PL/Python function "get_psutil_mem"
Below is the successful execution of psutil
root@ubuntu:~# python
Python 3.5.2 (default, Jan 26 2021, 13:30:48)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.cpu_times()
scputimes(user=139677.25, nice=755.4, system=53408.11, idle=10956992.84, iowait=18110.06, irq=0.0, softirq=1294.34, steal=0.0, guest=0.0, guest_nice=0.0)
>>>
Python 3.5.2 (default, Jan 26 2021, 13:30:48)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.cpu_times()
scputimes(user=139677.25, nice=755.4, system=53408.11, idle=10956992.84, iowait=18110.06, irq=0.0, softirq=1294.34, steal=0.0, guest=0.0, guest_nice=0.0)
>>>
Function details:
CREATE OR REPLACE FUNCTION get_psutil_mem(
OUT total float8, OUT used float8, OUT free float8, OUT buff_cache float8, OUT available float8, OUT percent float8,
OUT swap_total float8, OUT swap_used float8, OUT swap_free float8, OUT swap_percent float8
)
LANGUAGE plpython3u
AS $FUNCTION$
from psutil import virtual_memory, swap_memory
vm = virtual_memory()
sw = swap_memory()
return vm.total, vm.used, vm.free, vm.buffers + vm.cached, vm.available, vm.percent, sw.total, sw.used, sw.free, sw.percent
$FUNCTION$;
OUT total float8, OUT used float8, OUT free float8, OUT buff_cache float8, OUT available float8, OUT percent float8,
OUT swap_total float8, OUT swap_used float8, OUT swap_free float8, OUT swap_percent float8
)
LANGUAGE plpython3u
AS $FUNCTION$
from psutil import virtual_memory, swap_memory
vm = virtual_memory()
sw = swap_memory()
return vm.total, vm.used, vm.free, vm.buffers + vm.cached, vm.available, vm.percent, sw.total, sw.used, sw.free, sw.percent
$FUNCTION$;
The above function is used by pgwatch2 to monitor memory.
I tried installing python3.4.3 from source code but still the same error. Also tried most of the solutions provided on the internet but nothing helped. Problem is with the psutil module only.
Please let me know if I am missing anything.
Any help will be much appreciated.
Regards,
Ganesh Korde.
On 8/1/21 7:40 AM, Ganesh Korde wrote: > Hi Everyone, > > Environment > OS : Linux ubuntu 4.4.0-87-generic > Database: PostgreSQL 10.7 on x86_64-pc-linux-gnu, > Extension: plpython3u > Python version: Python 3.5.2 > > I am trying to run a python function using the language plpython3u and > I am getting the below error, though I have installed psutil. > > postgres=# select * from get_psutil_mem(); > ERROR: ImportError: No module named 'psutil' > CONTEXT: Traceback (most recent call last): > PL/Python function "get_psutil_mem", line 2, in <module> > from psutil import virtual_memory, swap_memory > PL/Python function "get_psutil_mem" > > The above function is used by pgwatch2 to monitor memory. > > *I tried installing python3.4.3 from source code but still the same > error.* Also tried most of the solutions provided on the internet but > nothing helped. Problem is with the psutil module only. > > Please let me know if I am missing anything. How did you install plpython3u? How did you install psutil? > > Any help will be much appreciated. > > Regards, > Ganesh Korde. -- Adrian Klaver adrian.klaver@aklaver.com
Adrian Klaver <adrian.klaver@aklaver.com> writes: > On 8/1/21 7:40 AM, Ganesh Korde wrote: >> I am trying to run a python function using the language plpython3u and >> I am getting the below error, though I have installed psutil. >> >> postgres=# select * from get_psutil_mem(); >> ERROR: ImportError: No module named 'psutil' > How did you install plpython3u? > How did you install psutil? Experimenting locally, I observe something that seems relevant: I get that error message phrasing from python *2*. In python 3, an unknown module name draws "ModuleNotFoundError": $ python3 Python 3.6.8 (default, Mar 18 2021, 08:58:41) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from z import virtual_memory, swap_memory Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'z' Now it's possible that the wording didn't change exactly at the 2/3 boundary, but I doubt it. So it looks to me like the alleged plpython3u language is actually invoking python 2.something, which is unlikely to work well at all. regards, tom lane
On 8/1/21 8:51 AM, Tom Lane wrote: > Adrian Klaver <adrian.klaver@aklaver.com> writes: > > Now it's possible that the wording didn't change exactly at the 2/3 > boundary, but I doubt it. So it looks to me like the alleged plpython3u > language is actually invoking python 2.something, which is unlikely > to work well at all. In that vein the OP could run: DO $$ import sys plpy.notice(sys.version) $$ LANGUAGE plpython3u; in the database to see what plpython3u is actually pointing at? > > regards, tom lane > -- Adrian Klaver adrian.klaver@aklaver.com
Adrian Klaver <adrian.klaver@aklaver.com> writes: > In that vein the OP could run: > DO $$ > import sys > plpy.notice(sys.version) > $$ LANGUAGE plpython3u; > in the database to see what plpython3u is actually pointing at? +1 ... looking at sys.path in the same way would also be useful. regards, tom lane
Ok, thanks guys. Let me check that.
On Sun, Aug 1, 2021 at 10:05 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Adrian Klaver <adrian.klaver@aklaver.com> writes:
> In that vein the OP could run:
> DO $$
> import sys
> plpy.notice(sys.version)
> $$ LANGUAGE plpython3u;
> in the database to see what plpython3u is actually pointing at?
+1 ... looking at sys.path in the same way would also be useful.
regards, tom lane