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.


Solution 1:

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

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

Solution 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 ""

Solution 3:

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

Install as pip install py-cpuinfo

Use from the commandline: python -m cpuinfo

Code:

import cpuinfo
cpuinfo.get_cpu_info()['brand']