2

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?

6
  • Is requests installed for Python 3 in the container? FYI, in your CALL example requests is spelled wrong. Commented Sep 28, 2020 at 19:32
  • 1
    Don't you need to use pip3, not pip, to install for python3? Maybe that depends on your OS and how you installed python. Commented Sep 28, 2020 at 20:21
  • @AdrianKlaver Thanks for correct my typo, but problem still persist Commented Sep 29, 2020 at 4:07
  • @jjanes Not work, you can check my error msg Commented Sep 29, 2020 at 4:08
  • You didn't answer the first question, is requests installed for Python 3 in the Docker container? Commented Sep 29, 2020 at 4:18

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.