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

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. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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