Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 49 ยท Getting Started

Installing NumPy and Using Jupyter

Install NumPy 2 in a virtual environment with pip or uv, check the version, start JupyterLab, and learn notebook habits that keep numerical work reproducible.

Reading about arrays is one thing; having NumPy on your own computer is what lets you load your files, run longer jobs and build real projects. The setup takes about five minutes. This lesson shows the safe way to do it: a virtual environment for each project, NumPy installed with pip, and Jupyter for interactive work.

If you are following the pandas course too, the steps are the same, and one environment can serve both. See Installing pandas and Using Jupyter if you want the notebook basics from the data-analysis side.

Check your Python first

NumPy 2 supports recent Python versions only. Open a terminal and run:

Terminal
python --version

(Use python3 if python is not found.) Python 3.11 or newer is a safe choice. If yours is older, install a current release from python.org, or let a tool such as uv or Miniconda install one for you.

Create a virtual environment and install NumPy

A virtual environment keeps one project's packages apart from every other project. That matters for numerical code, because libraries such as NumPy, SciPy and Matplotlib must be compatible with each other.

Terminal
mkdir array-lab
cd array-lab
python -m venv .venv

# activate it
source .venv/bin/activate          # macOS and Linux
.venv\Scripts\Activate.ps1         # Windows PowerShell

python -m pip install numpy

Using python -m pip instead of plain pip guarantees that pip belongs to the Python you are running. If you prefer uv, the equivalents are uv venv and uv pip install numpy.

NumPy ships as a pre-compiled wheel for common systems, so no C compiler is needed. A typical data-science setup adds a few companions:

Terminal
python -m pip install numpy scipy matplotlib pandas jupyterlab

Verify the installation

Run the example below to see what a healthy installation reports. It prints the NumPy version, creates an array, and shows the default integer type, which depends on the platform (the browser runner uses 32-bit integers, most desktops 64-bit):

Python Is NumPy working?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In your own terminal the same lines should print a NumPy 2.x version number. Two other commands help when you need to report a problem:

Terminal
python -c "import numpy; print(numpy.__version__)"
python -c "import numpy; numpy.show_config()"

show_config() lists the maths libraries (BLAS and LAPACK) NumPy was built with. These libraries do the heavy lifting for matrix products, so their presence explains why @ is so fast.

To record exactly what you installed and recreate it elsewhere:

Terminal
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt

Starting Jupyter

Jupyter is a notebook interface: you write code in cells, run a cell with Shift+Enter and see arrays, tables and plots right below it. It suits numerical experiments because you can change one line and rerun just that cell.

Terminal
python -m pip install jupyterlab
jupyter lab

A browser tab opens. Click Python 3 under Notebook. Type this in the first cell and press Shift+Enter:

Python
import numpy as np
np.arange(5) ** 2

The value of the last expression in a cell is displayed automatically, which is why you often see arrays at the bottom of notebook cells without print.

flowchart LR
    C["Cell:<br/>code you type"] -->|"Shift+Enter"| K["Kernel<br/>holds your arrays"]
    K -->|"output below the cell"| C
    K --- M[("Memory:<br/>all variables")]

Cells send code to the kernel, which keeps every variable in memory between runs.

Notebook habits for numerical work

Notebooks are friendly but easy to misuse. These habits avoid most problems:

  • Restart and run all. The kernel remembers variables from cells you may have deleted or run out of order. Before you trust a result, restart the kernel and run all cells from the top.
  • One import cell. Put import numpy as np and other imports at the top, once.
  • Set the random seed in the same cell that uses it. With rng = np.random.default_rng(42) at the top of a cell, rerunning the cell gives the same numbers.
  • Control printing. Large arrays are summarized with .... Use np.set_printoptions(precision=3, suppress=True, linewidth=100) to make numbers readable.
  • Time cells. Put %timeit expr in a cell to measure a short expression, or %%time on the first line to time the whole cell. These are Jupyter "magic" commands, not Python, so they work only in notebooks.
  • Watch memory. Arrays stay in memory as long as the kernel lives. Use del big to free large temporary arrays, or restart the kernel.

The example shows print options at work, and how you would check memory use of an array:

Python Print options and memory size

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Other places to run NumPy

  • VS Code can open .ipynb notebooks and lets you select your .venv interpreter. You get Jupyter's workflow with an editor's search and debugging.
  • Hosted notebooks, such as Google Colab, have NumPy preinstalled and need no setup. Your files must be uploaded, and the session is temporary.
  • Scripts (python analysis.py) are the right choice for long jobs, scheduled tasks and anything that should run unattended.
  • Conda (conda install numpy) manages Python and compiled libraries together; use it if your team already does.

When something goes wrong

  • ModuleNotFoundError: No module named 'numpy': you installed NumPy into one Python and are running another. Activate the environment and use python -m pip install numpy. In a notebook, install jupyterlab inside the same environment.
  • Version conflicts after an upgrade: older packages built for NumPy 1.x may fail with NumPy 2. Upgrade those packages, or pin numpy<2 in that project as a temporary measure.
  • Name clash: never call your own file numpy.py or random.py. Python would import your file instead of the library, producing strange errors.
  • Stuck notebook cell: use Kernel, Interrupt. If that fails, restart the kernel.

Tip: Include the output of np.__version__ and python --version when you ask a question on a forum. Many array bugs depend on the version.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use python -m pip install numpy instead of just pip install numpy?

    Several Python installations can exist on one machine. python -m pip uses the pip of exactly that interpreter, which avoids installing into the wrong one.
  2. 2. In a notebook, what do you see when the last line of a cell is the expression a * 2?

    Jupyter displays the value of the last expression in a cell automatically.
  3. 3. Why should you restart the kernel and run all cells before trusting results?

    The kernel keeps variables from earlier runs. A clean run shows whether the notebook really reproduces its results.
  4. 4. You named your own file numpy.py and import numpy as np now fails strangely. What is the cause?

    Python searches the current folder first, so a file called numpy.py shadows the real library. Rename your file.

Summary

  • Use a recent Python, one virtual environment per project, and python -m pip install numpy.
  • Check with np.__version__; np.show_config() shows the BLAS and LAPACK libraries behind matrix products.
  • JupyterLab runs code in cells on a kernel that keeps your arrays in memory; the last expression is displayed.
  • Restart and run all to keep notebooks reproducible; seed your random generator in the cell that uses it.
  • np.set_printoptions tidies output; %timeit and %%time measure speed, and nbytes shows memory use.
  • Never name your own file after a library, and keep notebook kernels and pip in the same environment.

Next: Your First Arrays starts the real array work.