Installing & Importing NumPy
How to install NumPy with pip, import it with the standard alias np, check the installed version, and fix the most common setup errors.
Before you can score runs you need to walk out to the crease. Before you can use NumPy you need to install it (once per Python environment) and import it (once per script or notebook).
Definition
Installing a package downloads it from PyPI (the Python Package Index) into your Python environment, using
pip. Importing a package loads it into the current program's memory so its functions can be used. The NumPy community convention is to import it under the short aliasnp.
- pip — Python's package installer. It comes with Python.
- Environment — a particular Python installation (system Python, a virtual environment, a conda env). A package installed in one environment is not visible in another.
- Alias — a nickname given with
as.import numpy as nplets you writenp.arrayinstead ofnumpy.array.
Syntax
python
# In a terminal / command prompt (not inside Python):
pip install numpy # install the latest version
pip install --upgrade numpy # upgrade an existing install
pip show numpy # show installed version and location
# In a Jupyter / Colab notebook cell:
%pip install numpy
# In your Python code:
import numpy as np # the standard way
print(np.__version__) # check the version| Command / code | What it does | Where you type it |
|---|---|---|
pip install numpy | Downloads and installs NumPy | Terminal / CMD / PowerShell |
python -m pip install numpy | Same, but guarantees it uses this python | Terminal (safest on Windows) |
%pip install numpy | Installs into the notebook's own kernel | Jupyter / Colab cell |
import numpy as np | Loads NumPy with the alias np | Top of every .py file / notebook |
np.__version__ | A string with the installed version | Python code |
Note: Google Colab, Anaconda, Kaggle notebooks and this site's browser playground already include NumPy — you can skip the install step there and go straight to
import numpy as np.
Example — import and check the version
python
import numpy as np
print(np.__version__)text
2.4.6How it works: np.__version__ is a plain string attribute. Your version may be different — any NumPy 2.x works for this course. If you see a 1.x version, some printing details differ (for example, NumPy 2 shows a scalar as np.int64(72) in a notebook, while NumPy 1 showed 72), so upgrading is recommended.
Why np?
Every tutorial, Stack Overflow answer, documentation page and library uses np. Using the same alias makes your code instantly readable to everyone.
| Import style | Example call | Verdict |
|---|---|---|
import numpy as np | np.array([1, 2]) | ✅ Standard — always use this |
import numpy | numpy.array([1, 2]) | Works, but long |
from numpy import * | array([1, 2]) | ❌ Avoid — overwrites built-ins like sum, min, max, round |
import numpy as n | n.array([1, 2]) | ❌ Confusing for other readers |
Warning:
from numpy import *replaces Python's built-insum,max,min,abs,roundand others with NumPy versions that behave slightly differently. This causes very confusing bugs.
Common errors and fixes
| Error | Cause | Fix |
|---|---|---|
ModuleNotFoundError: No module named 'numpy' | NumPy not installed in this environment | python -m pip install numpy (or %pip install numpy in a notebook) |
NameError: name 'np' is not defined | You forgot the import, or didn't run the import cell | Add/run import numpy as np first |
'pip' is not recognized ... (Windows) | pip not on PATH | Use python -m pip install numpy or py -m pip install numpy |
AttributeError: module 'numpy' has no attribute ... | You named your own file numpy.py, which shadows the real library | Rename your file (and delete any __pycache__) |
Common mistakes
- ❌ Typing
pip install numpyinside a Python script or the>>>prompt. ✅ Run it in a terminal; in notebooks use%pip install numpy. - ❌ Saving your practice file as
numpy.py. ✅ Give it a different name likenumpy_practice.py. - ❌ Importing NumPy in the middle of a notebook and running cells out of order. ✅ Put
import numpy as npin the very first cell and run it first.
Interview tip
Q: You installed NumPy but still get ModuleNotFoundError. Why?
A: It was installed into a different Python environment from the one running your code (e.g. system Python vs a virtual environment or Jupyter kernel). Install with python -m pip install numpy using the same interpreter, or %pip install numpy inside the notebook.
Key points to remember
- Install once per environment with pip install numpy (or python -m pip install numpy); in notebooks use %pip install numpy.
- Import once per script with the standard alias: import numpy as np.
- np.__version__ gives the installed version as a string; this course uses NumPy 2.x.
- Colab, Anaconda, Kaggle and this site's playground already have NumPy installed.
- Avoid from numpy import * — it overwrites built-ins like sum, min, max and round.
- Never name your own file numpy.py — it hides the real library.
Try it yourself
Import NumPy and check the version
Why not from numpy import *
Practice: your setup check
Revision Flashcards
Tap a card to flip it and see the answer.
