Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are import statements in plpython handled?

I have a plypython function which does some json magic. For this it obviously imports the json library.

Is the import called on every call to the function? Are there any performance implication I have to be aware of?

like image 511
Mauli Avatar asked Feb 22 '13 11:02

Mauli


People also ask

Where should import statements be placed in a program?

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

What does the import statement do in Python?

The import statement allows you to import one or more modules into your Python program, letting you make use of the definitions constructed in those modules.

What is import and from import statement?

The import statement allows you to import all the functions from a module into your code. Often, though, you'll only want to import a few functions, or just one. If this is the case, you can use the from statement. This lets you import only the exact functions you are going to be using in your code.

What happens when you import a module Python?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.


2 Answers

The import is executed on every function call. This is the same behavior you would get if you wrote a normal Python module with the import statement inside a function body as oppposed to at the module level.

Yes, this will affect performance.

You can work around this by caching your imports like this:

CREATE FUNCTION test() RETURNS text
LANGUAGE plpythonu
AS $$
if 'json' in SD:
    json = SD['json']
else:
    import json
    SD['json'] = json

 return json.dumps(...)
$$;

This is admittedly not very pretty, and better ways to do this are being discussed, but they won't happen before PostgreSQL 9.4.

like image 197
Peter Eisentraut Avatar answered Oct 02 '22 13:10

Peter Eisentraut


The declaration in the body of a PL/Python function will eventually become an ordinary Python function and will thus behave as such. When a Python function imports a module for the first time the module is cached in the sys.modules dictionary (https://docs.python.org/3/reference/import.html#the-module-cache). Subsequent imports of the same module will simply bind the import name to the module object found in the dictionary. In a sense, what I'm saying may cast some doubt on the usefulness of the tip given in the accepted answer, since it makes it somewhat redundant, as Python already does a similar caching for you.

To sum things up, I'd say that if you import in the standard way of simply using the import or from [...] import constructs, then you need not worry about repeated imports, in functions or otherwise, Python has got you covered.

On the other hand, Python allows you to bypass its native import semantics and to implement your own (with the __import__() function and importlib module). If this is what you're doing, maybe you should review what's available in the toolbox (https://docs.python.org/3/reference/import.html).

like image 42
Michael Ekoka Avatar answered Oct 02 '22 12:10

Michael Ekoka