Sam
Sam

Reputation: 3

Jupyter Notebook: Importing function from local module and external modules not defined

Using Jupyter notebooks, I wish to import a local function from a .py file that utilises functions from an external package (numpy). I am able to successfully import the function, however the imported function seems to not have access to other functions. It should be noted that within the notebook environment I am able to use the numpy functions as expected and np.linspace(3,10,10) outputs the correct array.

Below is a simple function made (in .py file in same directory) to demonstrate the problem:

def numpy_ran(x):
    print('---> TEXT TO SHOW FUNCTION SUCCESSFULLY IMPORTED <---')
    return np.linspace(x,10,10)

Below is the Jupyter notebook cell contents:

from <name of my file> import numpy_test
import numpy as np
numpy_ran(3)

And finally the error message:

---> TEXT TO SHOW FUNCTION SUCCESSFULLY IMPORTED <---

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-295710e81feb> in <module>
----> 1 numpy_ran(3)

~\Documents\sam\GANx\numpytest.py in numpy_ran(x)
      1 def numpy_ran(x):
      2     print('function is loading properly')
----> 3     return np.linspace(x,10,10)

NameError: name 'np' is not defined

I have added a screenshot to explain the issue further (not enough rep to embed the image directly).

Any help would really be appreciated, thanks in advance.

Sam

Upvotes: 0

Views: 1136

Answers (2)

Abhimanyu Shekhawat
Abhimanyu Shekhawat

Reputation: 186

Please add

import numpy as np

to numpytest.py.

Upvotes: 1

stelaespindola
stelaespindola

Reputation: 3

You have to import numpy in the other file as well.

Upvotes: 0

Related Questions