1

Code below works as expected. It prints 5 random numbers.

import numpy as np

class test_class():
    def __init__(self):
        self.rand_nums = self.create_rand_num()

    def create_rand_num(self):
        numbers = np.random.rand(5)
        return numbers

myclass = test_class()
myclass.rand_nums

However, the following does not work. NameError: name 'np' is not defined

import numpy as np
from test.calc import create_rand_num

class test_class():
    def __init__(self):
        self.rand_nums = create_rand_num()

myclass = test_class()
myclass.rand_nums

# contents of calc.py in test folder:
def create_rand_num():
    print(np.random.rand(5))

But, this works:

from test.calc import create_rand_num

class test_class():
    def __init__(self):
        self.rand_nums = create_rand_num()

myclass = test_class()
myclass.rand_nums

# contents of calc.py in test folder:
import numpy as np
def create_rand_num():
    print(np.random.rand(5))

Why must I have 'import numpy as np' inside calc.py? I already have this import before my class definition. I am sure I am misunderstanding something here, but I was trying to follow the general rule to have all the import statements at the top of the main code.

What I find confusing is that when I say "from test.calc import create_rand_num," how does Python know whether "import numpy as np" is included at the top of calc.py or not? It must know somehow, because when I include it, the code works, but when I leave it out, the code does not work.

EDIT: After reading the response from @DeepSpace, I want to ask the following:

Suppose I have the following file.py module with contents listed as shown:

import numpy as np
import pandas as pd
import x as y

def myfunc():
   pass

So, if I have another file, file1.py, and in it, I say from file.py import myfunc, do I get access to np, pd, and y? This is exactly what seems to be happening in my third example above.

In my third example, notice that np is NOT defined anywhere in the main file, it is only defined in calc.py file, and I am not importing * from calc.py, I am only importing create_rand_num. Why do I not get the same NameError error?

1 Answer 1

2

Python is not like C. Importing a module does not copy-paste its source. It simply adds a reference to it to the locals() "namespace". import numpy as np in one file does not make it magically available in all other files.

You have to import numpy as np in every file you want to use np.

Perhaps a worthwhile reading: https://docs.python.org/3.7/reference/simple_stmts.html#the-import-statement

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I thought 'from module import func', would only import func, nothing else. Why would it matter if I included 'import numpy as np' at the top of func def? I should still get the NameError...
@Murchak Because unless you import numpy as np in the same file, np is not defined
I think that much is clear to me. I want to clarify: if I import a particular function from a file.py module, do I get access to all the imported modules listed in the file.py module, or does the import only import the function and nothing else? I am sorry, I do not know how to clarify my question any further.
Thanks @DeepSpace. Just to wrap this up: If I have a class, contained in myclass.py file, that uses a whole bunch of custom functions contained in different .py files, I should import the needed modules in each of those files, instead of taking them out and putting them in the myclass.py file (that contains the code for my class)? Is this correct?

Your Answer

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.