SciPy Get Started

This page walks through installing SciPy, checking that the install worked, and understanding how its subpackages are imported.

Installing SciPy

  • pip install scipy - the standard way from PyPI
  • conda install scipy - if you manage packages with Anaconda or Miniconda
  • python -m pip install --upgrade scipy - to upgrade an existing install

SciPy depends on NumPy, so installing SciPy will pull in a compatible NumPy version automatically if one is not already present. You do not need to install NumPy separately first.

Checking Your Installation

Check the version

import scipy
print(scipy.__version__)

Importing scipy on its own only gives you a small set of top-level items, such as the version string and testing helpers. It does not automatically load scipy.optimize, scipy.sparse, or any other subpackage.

Note: Calling scipy.optimize.minimize(...) right after import scipy will raise an AttributeError in many SciPy versions. Always import the subpackage you need directly, for example from scipy import optimize.

Import and use a subpackage

from scipy import optimize, constants

print(constants.speed_of_light)  # 299792458.0

result = optimize.minimize(lambda x: (x - 3) ** 2, x0=0)
print(result.x)  # array([3.])

A Typical SciPy Workflow

  1. Import NumPy for arrays and basic math
  2. Import the specific SciPy subpackage(s) your task needs
  3. Prepare your input data as NumPy arrays
  4. Call the relevant SciPy function
  5. Read the result from the returned object's attributes, such as .x or .fun
TaskSubpackage to Import
Minimize a function or solve an equationscipy.optimize
Work with matrices mostly full of zerosscipy.sparse
Look up a constant or convert unitsscipy.constants
Do linear algebra beyond NumPy's basicsscipy.linalg
Note: Inside a Python shell, help(optimize.minimize) or dir(optimize) is a fast way to discover what a subpackage offers before you go looking through the documentation.

Exercise: SciPy Get Started

Which command installs SciPy using pip?