0
votes

I have a Jupyter notebook I am working in. In the first cell, I import several common packages:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal

In the next cell I import a package called "apt_pkg", which contains a module named "test_mod.py":

from apt_pkg import *

The module "test_mod.py" contains the following function:

def working():
   print('Is working')
   print(np.nan)
   return

Then I run the function in the Jupyter notebook:

test_mod.working()

When I do this, I get a NameError ("NameError: name 'np' is not defined"). The code can be seen here:

screenshot from Jupyter notebook

If I change the function and reference matplotlib.pyplot as plt (e.g. plt.show()) or pandas as pd (e.g. pd.DataFrame), I also get a NameError where 'plt' or 'pd' is not defined. How can I correct this?

1

1 Answers

0
votes

You need to define np inside test_mod.py:

import numpy as np

def working():
   print('Is working')
   print(np.nan)
   return

Importing numpy as np inside the Jupyter Notebook is not sufficient: you need to import the package in every script that needs it (here test_mod.py)