How to avoid loading a module multiple times?

Viewed 97

So I found a problem while "creating" modules. Let's say I create a nice module called foo in which I define a function named function that depends on numpy:

foo.py:

"""
This is foo a nice module
"""
import numpy as np

def function(parameter):
    return(np.zeros(parameter))

Then in another script I want to call my module:

import foo

So my problem is that numpy module is loaded inside foo so I can call it from foo (for example I can foo.np.zeros())

Is there any way that I'm not aware of in which the module will still work without having all numpy loaded inside it (naturally numpy should be loaded globally so foo works but i don't want it to be accessible from foo.np)

So far I've tried:

if __name__ == '__main__':
     import numpy as np

But this breaks the module foo even if numpy is loaded.

0 Answers
Related