Home > front end >  Why am I getting this 'NoneType' object has no attribute 'current' AttributeErro
Why am I getting this 'NoneType' object has no attribute 'current' AttributeErro

Time:09-21

So, I'm running a Linux VM on a macOS Catalina and trying to print on the screen some information about my machine's architecture.

import pygame
import psutil
import cpuinfo

# Shows CPU info
def mostra_info_cpu():
    s1.fill(branco)
    mostra_texto(s1, "Nome:", "brand", 10)
    mostra_texto(s1, "Arquitetura:", "arch", 30)
    mostra_texto(s1, "Palavra (bits):", "bits", 50)
    mostra_texto(s1, "Frequência (MHz):", "freq", 70)
    mostra_texto(s1, "Núcleos (físicos):", "nucleos", 90)
    tela.blit(s1, (0, 0))

# Shows info text according to the key('chave')
def mostra_texto(s1, nome, chave, pos_y):
    text = font.render(nome, True, preto)
    s1.blit(text, (10, pos_y))
    if chave == "freq":
        s = str(round(psutil.cpu_freq().current, 2))
    elif chave == "nucleos":
        s = str(psutil.cpu_count())
        s = s   " ("   str(psutil.cpu_count(logical=False))   ")"
    else:
        s = str(info_cpu[chave])
    text = font.render(s, True, cinza)
    s1.blit(text, (160, pos_y))

# gets CPU info
info_cpu = cpuinfo.get_cpu_info()

# screen colors
preto = (0, 0, 0)
branco = (255, 255, 255)
cinza = (100, 100, 100)

# starting initial screen
largura_tela = 800
altura_tela = 600
tela = pygame.display.set_mode((largura_tela, altura_tela))
pygame.display.set_caption("Informações de CPU")
pygame.display.init()

# surface to show information
s1 = pygame.surface.Surface((largura_tela, altura_tela))

pygame.font.init()
font = pygame.font.Font(None, 24)
    
# creating a clock
clock = pygame.time.Clock()
# time counter
cont = 60

terminou = False
# loop to capture events on the screen
while not terminou:
    # checking mouse events
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                terminou = True

    # updating every minute
    if cont == 60:
            mostra_info_cpu()
            cont = 0

    # updates screen
    pygame.display.update()

    # 60 frames por segundo
    clock.tick(60)
    cont = cont   1

pygame.display.quit()

The thing is I get this error when I run the program:

pygame 2.0.1 (SDL 2.0.14, Python 3.8.10)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "/home/kaiosilva/Desktop/INFNET/Segundo Bloco/Projeto em Arquitetura de Computadores, Sistemas Operacionais e Redes/kaio_henrique_PB_TP3.py", line 65, in <module>
    mostra_info_cpu()
  File "/home/kaiosilva/Desktop/INFNET/Segundo Bloco/Projeto em Arquitetura de Computadores, Sistemas Operacionais e Redes/kaio_henrique_PB_TP3.py", line 11, in mostra_info_cpu
    mostra_texto(s1, "Frequência (MHz):", "freq", 70)
  File "/home/kaiosilva/Desktop/INFNET/Segundo Bloco/Projeto em Arquitetura de Computadores, Sistemas Operacionais e Redes/kaio_henrique_PB_TP3.py", line 20, in mostra_texto
    s = str(round(psutil.cpu_freq().current, 2))
AttributeError: 'NoneType' object has no attribute 'current'

It seems to be a problem with the way I'm fetching CPU frequency. I haven't figured it yet.

I tried this code to get the architecture key-values:

import cpuinfo
info = cpuinfo.get_cpu_info()
for i in info:
        print(i, ":", info[i])

And got this output:

python_version : 3.8.10.final.0 (64 bit)
cpuinfo_version : [5, 0, 0]
arch : X86_64
bits : 64
count : 2
raw_arch_string : x86_64
vendor_id : GenuineIntel
brand : Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
hz_advertised : 3.1000 GHz
hz_actual : 3.1000 GHz
hz_advertised_raw : [3100000000, 0]
hz_actual_raw : [3100000000, 0]
stepping : 4
model : 61
family : 6
flags : ['3dnowprefetch', 'abm', 'adx', 'aes', 'apic', 'arat', 'arch_capabilities', 'arch_perfmon', 'avx', 'avx2', 'bmi1', 'bmi2', 'clflush', 'cmov', 'constant_tsc', 'cpuid', 'cpuid_fault', 'cx16', 'cx8', 'de', 'erms', 'f16c', 'flush_l1d', 'fma', 'fpu', 'fsgsbase', 'fxsr', 'hypervisor', 'ibpb', 'ibrs', 'invpcid', 'invpcid_single', 'lahf_lm', 'lm', 'mca', 'mce', 'md_clear', 'mmx', 'movbe', 'msr', 'mtrr', 'nonstop_tsc', 'nopl', 'nx', 'osxsave', 'pae', 'pat', 'pcid', 'pclmulqdq', 'pdpe1gb', 'pge', 'pni', 'popcnt', 'pse', 'pse36', 'pti', 'rdrand', 'rdrnd', 'rdseed', 'rdtscp', 'sep', 'smap', 'smep', 'ss', 'ssbd', 'sse', 'sse2', 'sse4_1', 'sse4_2', 'ssse3', 'stibp', 'syscall', 'tsc', 'tsc_adjust', 'tsc_deadline_timer', 'tsc_reliable', 'tscdeadline', 'vme', 'x2apic', 'xsave', 'xsaveopt', 'xtopology']
l3_cache_size : 4096 KB
l2_cache_size : 512 KiB
l1_data_cache_size : 64 KiB
l1_instruction_cache_size : 64 KiB
l2_cache_line_size : 6
l2_cache_associativity : 0x100
extended_model : 3

CodePudding user response:

check that cpu_freq() returns a valid response before trying to use it.

if chave = "freq":
    freq = psutil.cpu_freq()
    if freq:
        s = freq.current
    else:
        s = "unknown"
  • Related