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
- Install NumPy using one of the routes above, inside the environment you will use.
- At the top of your script or notebook cell, write
import numpy as np. - Call NumPy through the
np.prefix, for examplea = np.array([1, 2, 3]). - 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)
- NumPy installation: numpy.org/install, checked 2026-10-29.
- NumPy v2.5 Manual, absolute beginners: numpy.org/doc/stable/user/absolute_beginners.html, checked 2026-10-29.
- Python language reference, the import statement: docs.python.org/3/reference/simple_stmts.html, checked 2026-10-29.








