ProjectStack
python

OverflowError: Python int too large to convert to C long

A Python integer is too large to be converted to a C data type (like a 64-bit integer) that a library or system call requires. Python integers are unlimited in size, but C-backed libraries have fixed limits.

Common causes

  • Passing a very large Python int to a numpy, struct, or ctypes function that expects a fixed-size integer
  • A timestamp, ID, or counter exceeded the bounds of a 32-bit or 64-bit C integer
  • Calculations involving factorials or exponentials produced astronomically large numbers
  • Using math functions from the C standard library which have overflow limits

How to fix it

  1. Use numpy's appropriate dtype: np.int64(value) or check the dtype limits
  2. Reduce the value to a manageable range before passing it to C-backed functions
  3. For very large integer math, use Python's decimal module or keep values as Python ints
  4. Check input data for unexpectedly large values: print(value, type(value))

Example

Traceback (most recent call last): File "app.py", line 3, in <module> result = math.factorial(10000) File "<...>/math.py", line 1, in factorial OverflowError: Python int too large to convert to C long

Passing the result of a large factorial to a C-backed math function that can't handle numbers that large

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.