2

Overtime I have built up a collection of utility functions for various things.

I would like to put them all in package, with a bit more structure than just a single file containing all the functions.

Some of these functions are written assuming certain packages have been imported e.g. I have several numpy and pandas utility functions that assume something like import numpy as np

Obviously I will not use this hypothetical package like from <pkg> import * but I do not want to hinder performance either.

So if I have a numpy utility function, should I add this to every function

# mypkg.np.utils
import sys
def np_util_fn(...):
 if 'np' not in sys.modules: import numpy as np
 # rest of func

or

# mypkg.np.utils
import sys
if 'np' not in sys.modules: import numpy as np 
def np_util_fn(...):
 # rest of func

which is more performant if I use a different part of this package? e.g. from pkg.other.utils import fn

asked Nov 18, 2018 at 8:21

1 Answer 1

3

Ok, let's analyze your issue. Assume you have a file module.py:

print("Module got imported")

and a file test.py with:

import module
import module

. If you now execute test.py you will get

Module got imported

. Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np is not needed. This check only delays things as it may result in a double check.

In case you want to reimport a module you need reload(module). So if you have

import module
import module
reload(module)

in code.py you will see the line Module got imported two times.

This means that

import numpy as np

is sufficient. There is no need to check whether it already got imported via:

if 'np' not in sys.modules: import numpy as np

It depends whether it is advantageous to do import numpy as np at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.

I normally don't use any import statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.

answered Nov 18, 2018 at 8:37
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.