NameError: name 'np' is not defined (tested fix)

NameError: name 'np' is not defined (or 'pd') means the line import numpy as np never ran in the current scope. We reproduced the four ways that happens in Python 3.13.

1. The import line never ran

data = [3, 1, 2]
try:
    arr = np.array(data)          # forgot "import numpy as np"
except NameError as e:
    print(f"{type(e).__name__}: {e}")
try:
    df = pd.DataFrame(data)       # forgot "import pandas as pd"
except NameError as e:
    print(f"{type(e).__name__}: {e}")

Output:

NameError: name 'np' is not defined
NameError: name 'pd' is not defined

np and pd are not built into Python; they are names created by import numpy as np and import pandas as pd. If that line is missing, misspelled, below the code that uses it, or in a notebook cell you have not run since the kernel restarted, you get this NameError. The fix is to put the import at the top of the file (or run the import cell first).

2. A try/except around the import hides the real error

try:
    import numpy as np
except ImportError:
    pass                          # the real error is swallowed here
try:
    np.zeros(3)
except NameError as e:
    print(f"{type(e).__name__}: {e}")
try:
    import numpy as np
except ImportError as e:
    print(f"{type(e).__name__}: {e}")

Output:

NameError: name 'np' is not defined
ModuleNotFoundError: No module named 'numpy'

On our test box numpy is not installed. The except ImportError: pass hides that, so the failure shows up later as name 'np' is not defined. Remove the try/except (or re-raise) and you see the real cause: No module named 'numpy'. Install it for the interpreter you are actually running with python3 -m pip install numpy (or pandas).

3. Imported without the alias

import statistics                  # imported without an alias
try:
    st.mean([1, 2, 3])            # the alias was never created
except NameError as e:
    print(f"{type(e).__name__}: {e}")
print(statistics.mean([1, 2, 3]))
import statistics as st
print(st.mean([1, 2, 3]))

Output:

NameError: name 'st' is not defined
2
2

import numpy creates the name numpy, not np. We used statistics to show it because numpy is not installed here; the rule is the same for any module. Either write numpy.array(...) or import with as np.

4. Imported inside a function

def load():
    import statistics as st       # local to load() only
    return st.mean([1, 2, 3])
print(load())
try:
    st.median([1, 2, 3])
except NameError as e:
    print(f"{type(e).__name__}: {e}")

Output:

2
NameError: name 'st' is not defined

An import inside a function only creates a local name. Code elsewhere in the module cannot see it. Move the import to module level.

Related: NameError: name is not defined (all causes).

Frequently asked questions

Why is 'np' not defined?

np only exists after import numpy as np runs. Add it at the top of the file, or run the import cell first in a notebook.

I imported numpy but still get name 'np' is not defined

import numpy creates the name numpy, not np. Use import numpy as np, or write numpy.array.

How do I fix name 'pd' is not defined?

Add import pandas as pd at the top. If that raises No module named 'pandas', install it with python3 -m pip install pandas.

Written with AI assistance. Measured on 2026-10-22 on Debian with Python 3.13.5 (numpy and pandas are NOT installed on this box): missing imports, an import swallowed by try/except, an import without an alias (shown with statistics), and an import inside a function. Every block was run verbatim and its output pasted unedited. PlainHeaders is our extension; Python is free.