Skip to main content
v2026.11,858 entries · CC-BY 4.0

import numpy as np: What It Does and How to Fix Errors

What import numpy as np does, why the np alias is a convention, how to install NumPy, and how to fix NameError and import failures.

Written and maintained by CASRAI Editorial Board

Last updated

import numpy as np loads the NumPy library and binds it to the short name np, so every NumPy function is called as np.something. The alias is a convention, not a requirement: Python only needs the as clause, and the name you choose is the name you must use afterwards.

What it is used for in research

In research code the import line is usually the first line of an analysis script or notebook. The NumPy documentation describes the alias this way: the widespread convention allows access to NumPy features with a short, recognizable prefix (np.) while distinguishing NumPy features from others that have the same name.

For reproducible work, keep the import at the top of the script or the first notebook cell, so a reader sees at once which libraries the analysis depends on, and record the environment the code ran in. For what comes after the import, see the guides on NumPy array length, creating arrays with numpy.zeros, numpy.linspace and import matplotlib.pyplot as plt, and the wider overview Python for exploratory data analysis.

What does the as clause actually do?

The Python language reference states that if the module name is followed by as, the name following as is bound directly to the imported module. The reference’s own example is import foo.bar.baz as fbb. In import numpy as np, the module object for NumPy is therefore bound to np, and the name numpy itself is not defined in your code.

That has a direct consequence, and it is the most common source of beginner errors: after import numpy as np, calling numpy.array(...) fails because the name numpy was never bound, and after plain import numpy, calling np.array(...) fails because np was never bound.

Install or access

The official NumPy installation page (numpy.org/install) gives these routes.

Route Commands from numpy.org/install
pip pip install numpy
conda conda create -n my-env, then conda activate my-env, then conda install numpy (defaults or conda-forge channels)
Virtual environment, then pip python -m venv my-env, then source my-env/bin/activate (macOS and Linux), then pip install numpy

The install page recommends using a virtual environment for better dependency management. Install NumPy into the same environment you run your code from, otherwise the import will not find it.

Step-by-step: import and first array

  1. Install NumPy using one of the routes above, inside the environment you will use.
  2. At the top of your script or notebook cell, write import numpy as np.
  3. Call NumPy through the np. prefix, for example a = np.array([1, 2, 3]).
  4. Apply an operation, for example print(a * 2).
import numpy as np

a = np.array([1, 2, 3])
print(a * 2)

Output and how to read it

The import line itself prints nothing; silence means it succeeded. The example above prints [2 4 6]: the multiplication was applied to each element of the array, which is the behaviour that distinguishes a NumPy array from a plain Python list. The NumPy beginners’ guide (numpy.org/doc/stable/user/absolute_beginners.html) uses the same import numpy as np line as its starting point.

Troubleshooting

NameError: name ‘np’ is not defined

The alias was never bound in the running session. Check that the import line has been executed (in a notebook, that its cell has been run before the cell that uses np) and that the alias is spelled the same way in the import and in the calls.

NameError: name ‘numpy’ is not defined

You imported with the alias but called the library by its full name. Either use np. throughout or change the import to plain import numpy.

The import itself fails

The Python language reference notes that an import failure may indicate either that the module could not be located, or that an error occurred while initializing the module. If NumPy cannot be located, it is not installed in the environment that is running your code: activate the environment you installed into, or install NumPy there using the routes above.

Free alternatives (import styles)

NumPy is free to install. Several import forms reach the same library; they differ only in which names end up bound in your code.

Form Name bound in your code How you call it
import numpy as np np np.array([1, 2, 3])
import numpy numpy numpy.array([1, 2, 3])
from numpy import array array array([1, 2, 3])

The NumPy documentation’s reason for preferring the np. prefix is that it keeps NumPy names distinct from other names that are spelled the same. Importing array directly drops that distinction.

FAQ

What does np mean in import numpy as np?

It is the short name bound to the NumPy module. Nothing in Python gives np special meaning; it is the name the NumPy documentation uses by convention.

Do I have to use np?

No. Per the Python reference, the name after as is whatever you choose, but code that follows the np convention is easier for other researchers to read and for tutorials and documentation to match.

Why import numpy as np instead of import numpy?

The alias gives a short, recognizable prefix while still distinguishing NumPy functions from others with the same name, according to the NumPy documentation.

How do I install NumPy before importing it?

Use pip install numpy or conda install numpy, preferably inside a virtual environment, as described under Install or access. See also NumPy arrays for what to do next.

Sources (official docs, checked 2026-10-29)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about import numpy as np: What It Does and How to Fix Errors

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.