I do not know the right way to import modules.
I have a main file which initializes the code, does some preliminary calculations etc.
I also have 5 functions f1, f2, … f5. The main code and all functions need Numpy.
If I define all functions in the main file, the code runs fine.
(Importing with : import numpy as np)
If I put the functions in a separate file, I get an error:
Error : Global name 'linalg' is not defined.
What is the right way to import modules such that the functions f1 – f5 can access the Numpy functionality?
As other answers say, you need to import numpy into each file where you call a Numpy function. However you don’t need to import it into the main module if you’re not using it in the main module. Here’s a simple example. Imagine you have a file with your function in it called myFunc.py
myFunc.py:
Then in your main file you can do something like this
Your output will be:
So you pass a list to your function, convert it to a numpy array in your function, then return the answer as a list. If you return a numpy array you’ll get an error.