Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
CHAPTER GUIDE · GETTING STARTED

Packages and installation

On this pageWhy add a package?Install in the environment you useUse the installed package

Python already supplies tools for everyday tasks, such as displaying text and working with files. Other tasks benefit from additional packages. Installing a package makes its code available to a particular Python environment; importing it makes that code available inside a program.

Why add a package?

NumPy is a package for numerical work. Its central structure is an array: an organised collection of values on which calculations can be performed together. NumPy is useful for measurements, numerical datasets and calculations used in scientific computing.1

A greeting program needs no NumPy installation. A program that calculates with many numerical values may benefit from it. Packages are added when the problem calls for them, rather than simply because they exist.

Install in the environment you use

With Anaconda, open Anaconda Prompt on Windows or the configured Terminal on macOS/Linux. Select the intended environment, then use conda to install NumPy if it is needed:

conda install numpy
Listing 38 — Install NumPy in the active conda environment.

Conda displays a proposed package plan. Review it and confirm to proceed. It also handles dependencies—other packages needed by the requested package. If NumPy is already present, another installation is unnecessary.2

pip is Python’s package installer, commonly used to obtain packages from the Python Package Index (PyPI). For a Python environment managed with pip, the equivalent command is:

python -m pip install numpy
Listing 39 — Install NumPy using pip associated with the selected Python.

python -m pip runs pip through that interpreter, helping avoid installing into a different Python by mistake. Use the appropriate route; do not run both commands for the same installation. In a conda environment, prefer conda packages first and use pip for additional needs, rather than repeatedly mixing the two tools.3

Installation commands belong in the terminal, not in a .py file or at the >>> prompt.

Use the installed package

In a Python file or notebook using that same environment, enter:

import numpy as np

values = np.array([10, 20, 30])
print(values.mean())
Listing 40 — Calculate the mean of three values with NumPy.

import loads NumPy for this program. as np gives it the conventional short name np. The next line creates an array, and mean() calculates its arithmetic average. The output is 20.0.

If Python reports ModuleNotFoundError, check that the editor or notebook uses the environment in which NumPy was installed. To update NumPy later, use conda update numpy in the conda route, or python -m pip install --upgrade numpy in the pip route.

As projects acquire different package requirements, keeping their environments separate becomes useful.


  1. NumPy, What is NumPy?.↩︎

  2. conda, Managing packages.↩︎

  3. pip, User guide. Installation and upgrade syntax checked on 22 October 2026.↩︎