1

With Dockerfile I can run plpython3u perfectly

FROM postgres:11.3

RUN apt-get update && apt-get install -y postgresql-plpython3-11
CREATE OR REPLACE FUNCTION return_version()
  RETURNS VARCHAR
AS $$
    import sys
    return sys.version
$$ LANGUAGE plpython3u;

3.5.3 (default, Jul 9 2020, 13:00:10) [GCC 6.3.0 20170516]`

But I can't use dependencies for example requests

[38000] ERROR: ImportError: No module named 'requests' 
Where: Traceback (most recent call last): PL/Python function "return_version", line 3, 
in <module> import requests PL/Python function "return_version"

1st Attempt to do a call

CREATE OR REPLACE FUNCTION return_pip()
RETURNS VARCHAR AS $$
from subprocess import call
return call(["pip", "install", "requests"])
$$ LANGUAGE plpython3u STABLE ;

No hope

[38000] ERROR: FileNotFoundError: [Errno 2] No such file or directory: 'pip' Where: Traceback (most recent call last): PL/Python function "return_ls", line 3, in <module> return call(["pip", "install", "requuest"]) PL/Python function "return_ls", line 246, in c ...

2nd Attempt

CREATE OR REPLACE FUNCTION return_version()
  RETURNS VARCHAR
AS $$
    import sys
    from subprocess import call
    return call(["pip3", "install", "requests"])

    import requests
    res = requests.get('https://google.com')
    print(res.text)
    return res.text
$$ LANGUAGE plpython3u;

Got error

SELECT return_version()
[2020-09-29 10:59:44] [38000] ERROR: FileNotFoundError: [Errno 2] No such file or directory: 'pip3'
[2020-09-29 10:59:44] Where: Traceback (most recent call last):
[2020-09-29 10:59:44] PL/Python function "return_version", line 4, in <module>
[2020-09-29 10:59:44] return call(["pip3", "install", "requests"])
[2020-09-29 10:59:44] PL/Python function "return_version", line 246, in call
[2020-09-29 10:59:44] PL/Python function "return_version", line 675, in __init__
[2020-09-29 10:59:44] PL/Python function "return_version", line 1281, in _execute_child
[2020-09-29 10:59:44] PL/Python function "return_version"

I got similar person asking same question, but no answer

Question:
How to install and run python dependencies under plpython3u?

questionto42
  • 7,175
  • 4
  • 57
  • 90
joe
  • 8,383
  • 13
  • 61
  • 109

1 Answers1

1

Thanks to @AdrianKlaver for his answer

Follow this. I can get the response

  1. I pick this answer to be my image
  2. Execute CREATE EXTENSION plpython3u; in SQL shell
  3. Run pip3 install requests In OS(aka container)
  4. create function
    CREATE OR REPLACE FUNCTION return_version()
       RETURNS VARCHAR
    AS $$
     import requests
     res = requests.get('https://google.com')
     return str(res.text)
    $$ LANGUAGE plpython3u;
    
  5. SELECT return_version(); Check the result
joe
  • 8,383
  • 13
  • 61
  • 109