[Python] KeyError: ‘__builtins__’ in NumPy

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am using a lab instrument that returns data as a NumPy array, which I am in turn storing as part of a list with this structure:

    raw_data = [[int, numpy.ndarray, int, int]]


    I want to grab the biggest number from the second column of that NumPy array (many rows and 2 columns), so I wrote this code:

    # …code that collects data and writes it to the raw_data list…
    max_ccounts = raw_data[1][1].max(axis=0)[1]
    print(max_ccounts)


    But I constantly get the following error:

    Traceback (most recent call last):
    File "/usr/local/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
    File "/usr/local/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
    File "/home/kimfook-lee/Desktop/automated_TPI.py", line 75, in <module>
    max_ccounts = raw_data[1][1].max(axis=0)[1]
    KeyError: '__builtins__'


    I know that raw_data[1][1] points to a NumPy array because I printed it. I wasn’t sure if I was accessing the list elements properly so I tried to do it on my own in the terminal, using the exact same NumPy array that was printed:

    >>> raw_data1 = np.array([[7200, 876],[8000,840271]])
    >>> raw_data = [[0,raw_data1,10,10]]
    >>> max_ccounts = raw_data[0][1].max(axis=0)[1]
    >>> print(max_ccounts)
    840271


    Whenever I search for KeyError, I only find things related to dictionaries, and can’t find any information related to __builtins__ or NumPy.

    I added some temp variables and printed them to find the error.

    # …code that collects data and writes it to the raw_data list…
    tmp1 = raw_data[1]
    print('tmp1:', tmp1)
    tmp2 = tmp1[1]
    print('tmp2', tmp2)
    tmp3=tmp2.max(axis=0)
    print('tmp3', tmp3)
    tmp4 = tmp3[1]
    print('tmp4', tmp4)


    Now there is the same error with a different traceback.

    KeyError: '__builtins__'
    The above exception was the direct cause of the following exception:
    Traceback (most recent call last):
    File "/usr/local/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
    File "/usr/local/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
    File "/home/kimfook-lee/Desktop/automated_TPI.py", line 75, in <module>
    print('tmp1:', tmp1)
    RuntimeError: Unable to configure default ndarray.__repr__


    What is causing this error and how can it be resolved?

    Continue reading...

Compartilhe esta Página