Import globally and locally

I created a module that will be used in several python scripts. The structure is as follows:

Main file:

import numpy as np
from mymodule import newfunction
f = np.arange(100,200,1)
a = np.zeros(np.shape(f))
c = newfunction(f)

mymodule.py:

def newfunction(f):
    import numpy as np
    b = np.zeros(np.shape(f))
    return b

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

Ignore the functionality of this program, but the problem is that when I run it, I get "NameError: the global name" zeros "is not defined."

What did I miss here?

+3
source share
1 answer

mymodule.py does not see:

  import numpy as np

(s). "import" Python #include ++, , . "np" , .

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

- mymodule.py script, , , .

:

OP , import numpy as np , .

+3

Source: https://habr.com/ru/post/1731329/


All Articles