mpmath
raw JSON → 1.4.1 verified Tue May 12 auth: no python install: verified quickstart: verified
mpmath is a Python library for arbitrary-precision floating-point arithmetic, currently at version 1.4.1, with a release cadence of approximately every 1-2 years.
pip install mpmath Common errors
error ImportError: No module named mpmath ↓
cause The mpmath library is not installed in the current Python environment or the Python interpreter cannot find it. This can also manifest as 'ImportError: No module named mpmath.libmp'.
fix
Install the mpmath library using pip or conda:
pip install mpmath
or
conda install mpmath error AttributeError: module 'mpmath' has no attribute 'rational' ↓
cause This error typically occurs when a dependent library (like SymPy) expects an `mpmath.rational` attribute, but a newer version of mpmath (e.g., 1.4.0a0 or later) has removed or moved this attribute, leading to an API incompatibility.
fix
Pin the mpmath version to a compatible older release, such as 1.3.0, or update the dependent library if a version compatible with newer mpmath releases is available.
pip install mpmath==1.3.0 error TypeError: cannot create mpf from <value> ↓
cause This error occurs when attempting to convert an object of an unsupported type (e.g., a NumPy integer type like `numpy.int64`, a NumPy array, or other non-standard numeric types) directly into an `mpmath.mpf` (arbitrary-precision float) object. `mpmath` expects standard Python numbers, strings, or other `mpmath` types for direct conversion.
fix
Explicitly convert the input to a standard Python float or a string before passing it to
mpmath.mpf or an mpmath function. For NumPy types, cast to float first.
from mpmath import mpf
import numpy as np
# Original problematic code (example with numpy.int64)
# x_np_int = np.int64(1)
# m_val = mpf(x_np_int) # This would raise the TypeError
# Corrected code
x_np_int = np.int64(1)
m_val = mpf(float(x_np_int)) # Convert to Python float first
print(m_val)
# Example with a numpy array (common when plotting)
# import mpmath
# arr = np.array([1.0, 2.0, 3.0])
# mpmath.cos(arr) # This would raise the TypeError
# Corrected code for array element-wise operations
import mpmath
arr = np.array([1.0, 2.0, 3.0])
m_arr = [mpmath.cos(mpmath.mpf(x)) for x in arr]
print(m_arr) error TypeError: 'float' object cannot be interpreted as an integer ↓
cause An `mpmath.mpf` object (or sometimes a standard Python float that results from mpmath calculations) was passed to a Python function or operation that strictly requires an integer argument, such as Python's built-in `range()`, indexing a list, or `numpy.linspace`'s `num` argument.
fix
Convert the
mpmath.mpf object (or the float) to a standard Python integer using int() before passing it to functions or operations that expect an integer.
from mpmath import mp
# Original problematic code
# mp.dps = 15
# num_iterations_float = mp.sqrt(25.0)
# for i in range(num_iterations_float): # This would raise the TypeError
# print(i)
# Corrected code
mp.dps = 15
num_iterations_mpf = mp.sqrt(25.0) # This is an mpf object equivalent to 5.0
num_iterations_int = int(num_iterations_mpf) # Convert mpf to Python int
for i in range(num_iterations_int):
print(i)
# Example with numpy.linspace num argument
import numpy as np
# interval_len = mp.pi / 2
# x_values = np.linspace(0, float(mp.pi), num=interval_len) # This would raise TypeError if interval_len is mpf
# Corrected code
interval_len_mpf = mp.pi / 2 # This is an mpf object
interval_len_int = int(interval_len_mpf.real) # Convert mpf to Python int (using .real for complex mpf)
x_values = np.linspace(0, float(mp.pi), num=interval_len_int)
print(x_values) Warnings
gotcha Importing all functions with 'from mpmath import *' can lead to name conflicts with other libraries. It's recommended to import only the necessary functions or use the 'mpmath.' namespace. ↓
fix Use explicit imports like 'from mpmath import mp, mpf' or access functions via 'mpmath.'
gotcha Mixing mpmath with other libraries that define functions with the same names can cause unexpected behavior. To avoid this, import only the needed functions or use the 'mpmath.' namespace. ↓
fix Use explicit imports or access functions via 'mpmath.'
Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.10s 22.0M
3.10 slim (glibc) - - 0.07s 22M
3.11 alpine (musl) - - 0.17s 25.1M
3.11 slim (glibc) - - 0.14s 26M
3.12 alpine (musl) - - 0.15s 16.7M
3.12 slim (glibc) - - 0.15s 17M
3.13 alpine (musl) - - 0.14s 16.3M
3.13 slim (glibc) - - 0.15s 17M
3.9 alpine (musl) - - 0.10s 21.5M
3.9 slim (glibc) - - 0.08s 22M
Imports
- mp
from mpmath import mp - mpf
from mpmath import mpf
Quickstart verified last tested: 2026-04-23
from mpmath import mp
mp.dps = 50
print(mp.quad(lambda x: mp.exp(-x**2), [-mp.inf, mp.inf]) ** 2)