Getting processor information in Python

Posted on

Question :

Getting processor information in Python

Using Python is there any way to find out the processor information… (I need the name)

I need the name of the processor that the interpreter is running on. I checked the sys module but it has no such function.

I can use an external library also if required.

Answer #1:

The platform.processor() function returns the processor name as a string.

>>> import platform
>>> platform.processor()
'Intel64 Family 6 Model 23 Stepping 6, GenuineIntel'
Answered By: user225312

Answer #2:

Here’s a hackish bit of code that should consistently find the name of the processor on the three platforms that I have any reasonable experience.

import os, platform, subprocess, re

def get_processor_name():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        os.environ['PATH'] = os.environ['PATH'] + os.pathsep + '/usr/sbin'
        command ="sysctl -n machdep.cpu.brand_string"
        return subprocess.check_output(command).strip()
    elif platform.system() == "Linux":
        command = "cat /proc/cpuinfo"
        all_info = subprocess.check_output(command, shell=True).strip()
        for line in all_info.split("n"):
            if "model name" in line:
                return re.sub( ".*model name.*:", "", line,1)
    return ""
Answered By: user225312

Answer #3:

There’s some code here:

its very OS-dependent, so there’s lots of if-branches. But it does work out all the CPU capabilities.

$ python 
CPU information: getNCPUs=2 has_mmx has_sse has_sse2 is_64bit is_Intel is_Pentium is_PentiumIV

For linux it looks in /proc/cpuinfo and tries using uname. For Windows it looks like it uses the registry.

To get the [first] processor name using this module:

>>> import cpuinfo
>>>[0]['model name']
'Intel(R) Pentium(R) 4 CPU 3.60GHz'

If its got more than one processor, then the elements of will have their names. I don’t think I’ve ever seen a PC with two different processors though (not since the 80’s when you could get a Z80 co-processor for your 6502 CPU BBC Micro…)

Answered By: dbn

Answer #4:

For an easy to use package, you can use cpuinfo.

Install as pip install py-cpuinfo

Use from the commandline: python -m cpuinfo


import cpuinfo
Answered By: Spacedman

Answer #5:

I tried out various solutions here. cat /proc/cpuinf gives a huge amount of output for a multicore machine, many pages long, platform.processor() seems to give you very little. Using Linux and Python 3, the following returns quite a useful summary of about twenty lines:

import subprocess
print((subprocess.check_output("lscpu", shell=True).strip()).decode())
Answered By: serv-inc

Answer #6:

Working code (let me know if this doesn’t work for you):

import platform, subprocess

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        command = "cat /proc/cpuinfo"
        return subprocess.check_output(command, shell=True).strip()
    return ""
Answered By: cardamom

Answer #7:

The if-cases for Windows i.e platform.processor() just gives the description or family name of the processor e.g.
Intel64 Family 6 Model 60 Stepping 3.

I used:

  if platform.system() == "Windows":
        family = platform.processor()
        name = subprocess.check_output(["wmic","cpu","get", "name"]).strip().split("n")[1]
        return ' '.join([name, family])

to get the actual cpu model which is the the same output as the if-blocks for Darwin and Linux, e.g.
Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz Intel64 Family 6 Model 60 Stepping 3, GenuineIntel

Answered By: Phil L.

Answer #8:

Looks like the missing script in @Spacedman answer is here:

It is patched to work with Python 3.

CPU information: CPUInfoBase__get_nbits=32 getNCPUs=2 has_mmx is_32bit is_Intel is_i686

The structure of data is indeed OS-dependent, on Windows it looks like this:

>python -c "import cpuinfo, pprint; pprint.pprint([0])"
{'Component Information': 'x00x00x00x00x00x00x00x00x00x00x00x00x01x00x00x00',
 'Configuration Data': 'xffxffxffxffxffxffxffxffx00x00x00x00x00x00x00x00',
 'Family': 6,
 'FeatureSet': 2687451135L,
 'Identifier': u'x86 Family 6 Model 23 Stepping 10',
 'Model': 23,
 'Platform ID': 128,
 'Previous Update Signature': 'x00x00x00x00x0cnx00x00',
 'Processor': '0',
 'ProcessorNameString': u'Intel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz',
 'Stepping': 10,
 'Update Signature': 'x00x00x00x00x0cnx00x00',
 'Update Status': 2,
 'VendorIdentifier': u'GenuineIntel',
 '~MHz': 2394}

On Linux it is different:

# python -c "import cpuinfo, pprint; pprint.pprint([0])"
{'address sizes': '36 bits physical, 48 bits virtual',
 'apicid': '0',
 'bogomips': '6424.11',
 'bugs': '',
 'cache size': '2048 KB',
 'cache_alignment': '128',
 'clflush size': '64',
 'core id': '0',
 'cpu MHz': '2800.000',
 'cpu cores': '2',
 'cpu family': '15',
 'cpuid level': '6',
 'flags': 'fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm const
ant_tsc pebs bts nopl pni dtes64 monitor ds_cpl est cid cx16 xtpr pdcm lahf_lm',
 'fpu': 'yes',
 'fpu_exception': 'yes',
 'initial apicid': '0',
 'microcode': '0xb',
 'model': '6',
 'model name': 'Intel(R) Pentium(R) D CPU 3.20GHz',
 'physical id': '0',
 'power management': '',
 'processor': '0',
 'siblings': '2',
 'stepping': '5',
 'uname_m': 'x86_64',
 'vendor_id': 'GenuineIntel',
 'wp': 'yes'}
Answered By: oche

Leave a Reply

Your email address will not be published.