Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

NumPy has no function named factorial, so np.factorial(...) raises an AttributeError. To compute factorials of single numbers or whole arrays, use scipy.special.factorial from SciPy. Pass exact=True when you need exact integer results, and leave the default when a floating-point approximation is acceptable.

Why np.factorial fails

Running the call produces an error like this:

>>> import numpy as np
>>> np.factorial(5)
Traceback (most recent call last):
  ...
AttributeError: module 'numpy' has no attribute 'factorial'

The message means that the name factorial does not exist in the numpy namespace. It does not mean that NumPy’s arithmetic is broken. The NumPy 2.5 API reference, released June 28, 2026, groups its routines by topic and does not document a dedicated np.factorial function. That absence is an inference from reading the reference rather than a statement from a NumPy page about factorials, but it matches what you see when you run the call.

Before changing your code, rule out the common causes:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • The function was assumed to exist. Factorials are common enough that it is easy to expect NumPy to provide one. Import the function from SciPy instead.
  • A local file shadows the package. If your working directory contains a file named numpy.py, Python imports that file instead of the real package, and NumPy attributes appear to be missing. Check with python -c "import numpy; print(numpy.__file__)". If the path points into your project folder, rename the file and delete its __pycache__ folder.
  • Code runs under a different interpreter. Confirm which Python environment your editor or notebook uses before troubleshooting further.

Use scipy.special.factorial instead

SciPy documents scipy.special.factorial for scalar and array inputs. If SciPy is not installed in your environment, install it there:

python -m pip install scipy

Then import the function and pass it an array:

import numpy as np
from scipy.special import factorial

values = np.array([3, 4, 5])

exact_values = factorial(values, exact=True)
approx_values = factorial(values)  # exact=False by default

The SciPy 1.18.0 manual gives this example, and both modes return 6, 24 and 120 for the inputs 3, 4 and 5. The difference lies in how those values are produced and stored, which is what the next section covers.

Choose exact or approximate mode

The exact keyword decides the arithmetic path, and that choice determines the output type.

Need Call What you get Trade-off
Exact integer factorials factorial(values, exact=True) Integer results computed with integer arithmetic Output dtype is int64 or object, depending on magnitude
Fast floating-point approximation factorial(values) Floating-point results computed through the gamma function Values are approximations, not exact integers

exact=True: integer arithmetic

With exact=True, SciPy uses integer arithmetic. The output dtype widens when the values require it: int64 when that is enough, and object when it is not. Do not assume every exact result fits in a fixed-width NumPy integer. Once a factorial exceeds the range of int64, the array holds arbitrary-precision Python integers in an object array. Check result.dtype if the type matters to later code.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

exact=False: gamma-function approximation

The default mode computes factorials with the gamma function and returns floats. It is the appropriate choice when you need magnitudes or ratios and can accept floating-point rounding. Do not use it for counting, combinatorial identities, or equality tests on integer results, because the output is not guaranteed to be an exact integer.

Build a factorial table with cumprod

If you need factorials for a consecutive range that starts at 1, NumPy’s cumprod produces the running products directly:

import numpy as np

n = 10
table = np.cumprod(np.arange(1, n + 1))
print(table)  # [      1       2       6      24     120     720    5040   40320  362880 3628800]

This works only for a range beginning at 1, and it has a hard limit. NumPy stores these results as 64-bit integers, which cannot hold 21!, and NumPy wraps integer overflow silently rather than raising an error. For any table that reaches 21! or beyond, use scipy.special.factorial(..., exact=True) instead. This article does not establish a speed advantage for the cumulative-product approach, so choose it for clarity on small, consecutive ranges.

Troubleshooting checklist

  • AttributeError: module 'numpy' has no attribute 'factorial': replace the call with from scipy.special import factorial.
  • ModuleNotFoundError: No module named 'scipy': run python -m pip install scipy with the same interpreter that runs your script.
  • Results look like 6.0 instead of 6: you are using the default approximate mode. Pass exact=True.
  • Large values look wrong in a NumPy integer table: the table has overflowed. Switch to scipy.special.factorial with exact=True, and check that the output dtype is object.
  • Your code behaves differently from the examples here: confirm your version with python -c "import scipy; print(scipy.__version__)". The dtype behaviour described here comes from the SciPy 1.18.0 manual.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Notes on array support

Most functions in scipy.special accept NumPy arrays and follow NumPy’s broadcasting rules, and factorial is listed in that module. The module overview notes exceptions for some functions, so do not assume every function in it behaves identically on arrays. Verify the behaviour of any other special function you plan to use.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

NumPy’s own documentation describes universal functions as operating elementwise on arrays. That is the same array model that scipy.special.factorial follows in practice.

The Bottom Line

“”

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.