Python module: how to prevent importing modules called by the new module
Question: I am new in `Python` and I am creating a module to re-use some code. My module
(`impy.py`) looks like this (it has one function so far)...

    
    
    import numpy as np
    def read_image(fname):
        ....
    

and it is stored in the following directory:

    
    
    custom_modules/
                  __init.py__
                  impy.py
    

As you can see it uses the module numpy. The problem is that when I import it
from another script, like this...

    
    
    import custom_modules.impy as im
    

and I type `im.` I get the option of calling not only the function
`read_image()` but also the module `np`.

**How can I do to make it only available the functions I am writing in my
module and not the modules that my module is calling (numpy in this case)?**

Thank you very much for your help.


Answer: You could import numpy inside your function

    
    
    def read_image(fname):
        import numpy as np
        ....
    

making it locally available to the `read_image` code, but not globally
available.

Warning though, this might cause a performance hit (as `numpy` would be
imported each time the code is run rather than just once on the initial
import) - especially if you run `read_image` multiple times.

