0

I've tried searching with Google and in stackoverflow, but I can't find the answer to this simple question, that has probably been asked many times before

In file f1.py, I have

import numpy as np

In file f2.py, I have

import f1
class One:
    mat = np.array([[1]]))

When I run f2, I get the error message that 'np' is undefined. How should I express my obvious intentions? To give background, I am writing my first python3 program that is not a toy program. f1 represents the substantial code. f2 is supposed to be a unittest program. However, some fundamental misunderstanding of python syntax is preventing me from making progress. I thought that the import of f1 would bring np into the namespace of f2, but it doesn't seem to.

David Epstein
  • 433
  • 4
  • 14
  • 1
    Add `import numpy as np` to f2. Do _not_ import modules through another module (i.e. don't do `from f1 import np`), import them directly where you need them. – Aran-Fey Nov 09 '17 at 12:47
  • @dcg both star imports and importing names thru another module are considered bad practices. See Rawing's comment for the correct solution. – bruno desthuilliers Nov 09 '17 at 12:50
  • @brunodesthuilliers I know, the OP is showing that example but he's asking how to import names from a module not modules through another module. I'll remove the comment and post it edited. Thanks. – dcg Nov 09 '17 at 12:56
  • `from f1 import *` or just `from f1 import np`. Even though this works it's not a good practice to import a module through another module. See comments above. – dcg Nov 09 '17 at 12:56

3 Answers3

1

I thought that the import of f1 would bring np into the namespace of f2, but it doesn't seem to.

Indeed. Each module has it's own namespace, and you have to explicitely import all modules you depend on. So if f2.py needs numpy, it has to explicitely import it:

import numpy as np
import f1

class One:
    mat = np.array([[1]]))

NB : importing f1 doesn't directly injects any other name defined in f1 either, so if you have a function "foo" in f1, in f2 you'll need to either import f1 (as in the above example) and use the qualified name f1.foo() or explicitely import name "foo", ie from f1 import foo (but then you'll only get access to foo, not any of the other names defined in f1).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
0
mat = np.array([[1]])

In this line the np is not in local namespace because you have not imported np in local namespace.

So if you have to use np you should use following line:-

mat = f1.np.array([[1]])

For more info go to this link import x and from x import

-1

You should import np like this:

from f1 import np
Ayra Faceless
  • 229
  • 1
  • 7
  • Don't do this. If you want numpy in f2, import it explicitely in f2. – bruno desthuilliers Nov 09 '17 at 12:50
  • @brunodesthuilliers I don't want to ,but the question is asking how to import it from f1. – Ayra Faceless Nov 09 '17 at 13:32
  • the question is mostly asking why in f2 `np` is not defined after importing f1, which is not quite the same thing. And anyway, if someone asks how to do something the wrong way, it's better to tell him how to do it the right way instead... – bruno desthuilliers Nov 09 '17 at 13:46
  • @brunodesthuilliers Yeah, maybe he want to import something different, but the easiest way to do is import it explicitly and directly. I do think the questioner knows that. – Ayra Faceless Nov 09 '17 at 13:58
  • Well I wouldn't assume the OP knows that much, cf "I thought that the import of f1 would bring np into the namespace of f2" – bruno desthuilliers Nov 09 '17 at 14:18
  • @brunodesthuilliers haha, now he knows. – Ayra Faceless Nov 09 '17 at 16:11
  • I did know that I could write in f2.py 'import numpy as np', but had a (baseless) worry about using unnecessary space. Having read the interesting discussion (at least it was interesting for me, but presumably old hat to everyone else), I now think that the main point is to avoid an additional layer of indirection, and that's why numpy should be imported again. But I'll look at the link given by @user8751232 to see if there are additional lessons to be learned. – David Epstein Nov 10 '17 at 14:00