numpy/numpy · error · ValueError
mode must be one of {all_modes!r} (got {mode!r})
Error message
mode must be one of {all_modes!r} (got {mode!r}) What it means
Error "mode must be one of {all_modes!r} (got {mode!r})" thrown in numpy/numpy.
Source
Thrown at numpy/_core/memmap.py:225
>>> fpo = np.memmap(filename, dtype=np.float32, mode='r', offset=16)
>>> fpo
memmap([ 4., 5., 6., 7., 8., 9., 10., 11.], dtype=float32)
"""
__array_priority__ = -100.0
def __new__(cls, filename, dtype=uint8, mode='r+', offset=0,
shape=None, order='C'):
# Import here to minimize 'import numpy' overhead
import mmap
import os.path
try:
mode = mode_equivalents[mode]
except KeyError as e:
if mode not in valid_filemodes:
all_modes = valid_filemodes + list(mode_equivalents.keys())
raise ValueError(
f"mode must be one of {all_modes!r} (got {mode!r})"
) from None
if mode == 'w+' and shape is None:
raise ValueError("shape must be given if mode == 'w+'")
if hasattr(filename, 'read'):
f_ctx = nullcontext(filename)
else:
f_ctx = open(
os.fspath(filename),
('r' if mode == 'c' else mode) + 'b'
)
with f_ctx as fid:
fid.seek(0, 2)
flen = fid.tell()
descr = dtypedescr(dtype)View on GitHub (pinned to e117b3ca4e)
Solutions
- Use one of the valid modes: 'r', 'r+', 'w+', or 'c', e.g. np.memmap('f.dat', dtype='float64', mode='r', shape=(3,4)).
When it happens
Trigger: Passing an invalid mode string such as 'w' or 'a' to np.memmap.
Common situations: Assuming memmap accepts built-in open() modes like 'w' or 'rb'.
AI-assisted analysis of numpy/numpy@e117b3ca4e (2026-08-07).
Data as JSON: /api/errors/1d1e9da4bc559577.
Report an issue: GitHub.