3

I'm writing a series of plpythonu functions which share a few common bits of code. I'd like to package the shared code into a separate module for ease of maintenance. Is there an easy way to call my custom module from plpythonu?

I can't guarantee access to locations in Python's path so dropping it in a folder on the path may not work.

These are all part of a PostgreSQL extension so ideally I'd install the helper Python module along with the other extension files.

1 Answer 1

1

You might be able to do something along the lines of what was done in check_updates-1.0.1, if you can't make guarantees about being able to install in the system's PYTHONPATH.

In the Makefile, there is a substitution of based off the output of pg_config for the use lib directive of Perl. You could do something similar like this:

import sys
sys.path.append("<LIBDIR>/myextension")

in the Makefile of your extension to ensure that the install directory for modules is in the PYTHONPATH in your functions, no matter where someone has things installed.

You can cache your imports too, as referenced in this StackOverflow question: How are import statements in plpython handled, which should help with performance too.

It's not "easy", though, sadly. We have to wait for future improvements to plpythonu to make such things easier.

Hope that helps answer your question. =)

1
  • It turns out the answer about how import statements are handled in plpython you referenced was incorrect. See my comment there for more information. Commented Mar 26, 2020 at 23:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.