0

This might have been answered before, but I could not find anything that addresses my issue.

So, I have 2 files.

|
|-- test.py
|-- test1.py

test1.py is as below

def fnc():
    return np.ndarray([1,2,3,4])

I'm trying to call test1 from test and calling the function like

from test1 import *
x = fnc()

Now naturally I'm getting NameError: name 'np' is not defined.

I tried to write the import both in test and test1 as

import numpy as np

But still, I'm getting the error. This might be silly, but what exactly I'm missing?

Any help is appreciated. Thanks in advance.

lu5er
  • 3,229
  • 2
  • 29
  • 50
  • I can't reproduce it. Adding `import numpy as np` in *test1.py* fixes the error. – Georgy Dec 14 '18 at 09:48
  • @Georgy, when I'm running using cmd what you said is working fine.. But is there a limitation when using Jupyter for the same? – lu5er Dec 14 '18 at 09:51
  • Jupyter Notebook/Lab you mean? I don't understand how the presented example is applicable to Jupyter. As you accepted the answer below, does it mean that the issue is resolved? – Georgy Dec 14 '18 at 10:02

2 Answers2

1

Each Python module has it's own namespace, so if some functions in test1.py depends on numpy, you have to import numpy in test1.py:

# test1.py

import numpy as np

def fnc():
    return np.ndarray([1,2,3,4])

If test.py doesn't directly use numpy, you don't have to import it again, ie:

# test.py

# NB: do NOT use 'from xxx import *' in production code, be explicit
# about what you import

from test1 import fnc

if __name__ == "__main__":
    result = fnc()
    print(result)

Now if test.py also wants to use numpy, it has to import it too - as I say, each module has it's own namespace:

# test.py

# NB: do NOT use 'from xxx import *' in production code, be explicit
# about what you import

import numpy as np 
from test1 import fnc


def other():
    return np.ndarray([3, 44, 5])

if __name__ == "__main__":
    result1 = fnc()
    print(result1)

    result2 = other()
    print(result2)

Note that if you were testing your code in a python shell, just modifying the source and re-importing it in the python shell will not work (modules are only loaded once per process, subsequent imports fetch the already loaded module from the sys.modules cache), so you have to exit the shell and open a new one.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • Hey! Thanks for explaining. It was really helpful. – lu5er Dec 14 '18 at 09:59
  • You're welcome. And please do yourself a favor: do NOT use star imports (=> "from somemodule import *"), really (except of course in a python shell - and even then it might lead to unexpected results). – bruno desthuilliers Dec 14 '18 at 10:03
  • Sure. No one told me that before. But definitely, I'll keep that in mind. :) – lu5er Dec 14 '18 at 10:13
  • This is in pep08: "Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools" (https://www.python.org/dev/peps/pep-0008/#imports) - and I can testify that they do indeed confuse the reader AND easily break your code in rather unexpected ways. – bruno desthuilliers Dec 14 '18 at 11:27
0

mostly you need to have __init__.py in the directort where you have these files just try creating init.py file like below in the directory where you .py files are present and see if it helps.

touch __init__.py 
ShivYaragatti
  • 398
  • 2
  • 8
  • I'm sorry.. But it did not help. Can you give any references? – lu5er Dec 14 '18 at 09:52
  • @brunodesthuilliers thanks for correcting but in the question it is said that, tried "import numpy as np" files test1.py and test.py i couldn't undestand why? i read in the document that if we trying to import local ".py" we need to have __init__.py , though i am not very sure about, so i used "mostly" in the answer, i checked in my machine following works fine "from test1 import * x = fnc() print(x) " – ShivYaragatti Dec 14 '18 at 10:54
  • @ShivYaragatti `__init__.py` are required in python 2.x to turn a directory into a package. This has changed in Python 3 (cf https://stackoverflow.com/questions/37139786/is-init-py-not-required-for-packages-in-python-3). In both cases, they are not required to import a _module_ (simple .py file) - the only requirement here is that the directory containing the module is in the `sys.path`. But anyway: the error message posted by the OP - "NameError: name 'np' is not defined" - has nothing to do with importing the `test1.py` module itself. – bruno desthuilliers Dec 14 '18 at 11:24