59

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.

13 Answers 13

41

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

>>> import platform
>>> platform.processor()
'Intel64 Family 6 Model 23 Stepping 6, GenuineIntel'
Sign up to request clarification or add additional context in comments.

11 Comments

can anyone say whether this is platform independent output. ie. does it always give the exact same output for the same CPU on windows/linux/osx?
@gnibbler: Is there any reason to doubt that the output may vary? Though I hope someone can confirm.
@Spacedman: "An empty string is returned if the value cannot be determined." Have the same issue.
Note that many platforms do not provide this information or simply return the same value as for machine(). NetBSD does this.: I have this problem. I need to know if I'm running on intel or amd, since my scripts are generating configuration settings for compiling software, and depending on intel/amd I wanna set the xHost or msse3 option.
@JohnLaRooy I get only an 'x86_64' string running this on Ubuntu 18.04.
|
34

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).decode().strip()
        for line in all_info.split("\n"):
            if "model name" in line:
                return re.sub( ".*model name.*:", "", line,1)
    return ""

4 Comments

This is useful to find the right architecture even when python is running emulated. Thanks!
Why do you read files using subprocess and cat instead of the standard way? Is this for any special purpose?
oh, man! bc I wrote this to execute the commands on a remote machine and then parse locally
missing shell=True in platform.system() == "Darwin" branch
29

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']

6 Comments

This gives a lot better CPU information on MacOS than platform.processor() does
This does give the model name, but it's awefully slow because it computes other things you don't use here.
@NicoSchlömer: somehow, python and awfully slow happens often together (unless there's some specifically optimized code, like numpy) ;-)
No, it's got nothing to do with Python. The author of the package decided to perform some computation alongside fetching the information from a file.
Notably, it uses a giant hard-coded list of flags, so make sure you only rely on it for specific flags you're expecting; if, for example, you're trying to tell the user all their CPU flags, but they have a new architecture with features that were released since the library last got updated, you'll display erroneous information.
|
19

There's some code here:

https://github.com/pydata/numexpr/blob/master/numexpr/cpuinfo.py

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

$ python cpuinfo.py 
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
>>> cpuinfo.cpu.info[0]['model name']
'Intel(R) Pentium(R) 4 CPU 3.60GHz'

If its got more than one processor, then the elements of cpuinfo.cpu.info 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...)

6 Comments

Sorry I needed the name, I should have mentioned
The link seems broken and searching cpuinfo on that website brings lots of results. could you please put the project name/a link to code
@Moh Zah, I believe this should be the link to the project.
Want to see my Microchannel PS/2 with both 286 and 386 CPUs? ;)
In py-cpuinfo 5.0.0, the API seems to be cpuinfo.get_cpu_info()['brand']. Print cpuinfo.get_cpu_info() to check other keys of interest.
|
16

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())

1 Comment

Nice, this works well on embedded systems as well where you do not always have cpuinfo. Cheers
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 ""

1 Comment

In python3 subprocess won't return a str but a byte. You have to convert it into a str with your_byte.decode('utf-8'). For example, for Darwin the code would be model = subprocess.check_output(["/usr/sbin/sysctl", "-n", "machdep.cpu.brand_string"]).strip() ; return model.decode('utf-8').
6

[Answer]: this works the best

 import cpuinfo
 cpuinfo.get_cpu_info()['brand_raw'] # get only the brand name

or

 import cpuinfo
 cpuinfo.get_cpu_info()

To get all info about the cpu

{'python_version': '3.7.6.final.0 (64 bit)',
 'cpuinfo_version': [7, 0, 0],
 'cpuinfo_version_string': '7.0.0',
 'arch': 'X86_64',
 'bits': 64,
 'count': 2,
 'arch_string_raw': 'x86_64',
 'vendor_id_raw': 'GenuineIntel',
 'brand_raw': 'Intel(R) Xeon(R) CPU @ 2.00GHz',
 'hz_advertised_friendly': '2.0000 GHz',
 'hz_actual_friendly': '2.0002 GHz',
 'hz_advertised': [2000000000, 0],
 'hz_actual': [2000176000, 0],
 'stepping': 3,
 'model': 85,
 'family': 6,
 'flags': ['3dnowprefetch',
  'abm',
  'adx', ...more

Comments

4

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

Comments

2

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

https://github.com/pydata/numexpr/blob/master/numexpr/cpuinfo.py

It is patched to work with Python 3.

>python cpuinfo.py
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(cpuinfo.cpu.info[0])"
{'Component Information': '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00',
 'Configuration Data': '\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00',
 'Family': 6,
 'FeatureSet': 2687451135L,
 'Identifier': u'x86 Family 6 Model 23 Stepping 10',
 'Model': 23,
 'Platform ID': 128,
 'Previous Update Signature': '\x00\x00\x00\x00\x0c\n\x00\x00',
 'Processor': '0',
 'ProcessorNameString': u'Intel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz',
 'Stepping': 10,
 'Update Signature': '\x00\x00\x00\x00\x0c\n\x00\x00',
 'Update Status': 2,
 'VendorIdentifier': u'GenuineIntel',
 '~MHz': 2394}

On Linux it is different:

# python -c "import cpuinfo, pprint; pprint.pprint(cpuinfo.cpu.info[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'}

Comments

2

platorm.machine() for the ISA only

https://docs.python.org/3/library/platform.html#platform.machine can also be useful:

import platform
print(platform.machine())

possible output:

x86_64

Tested on Python 3.11, Ubuntu 23.04, Lenovo ThinkPad P51.

Number of CPUs

See: How to find out the number of CPUs using python

Comments

2

The simple answer to this is:

import cpuinfo
print(cpuinfo.get_cpu_info()["brand_raw"])

1 Comment

Worth mentioning that this is a library github.com/workhorsy/py-cpuinfo
1

As mentioned by @cardamom, linux command lscpu returns a lot of interesting information. Note that their is an option (-J, --json) to get the output in the JSON format. This make it much easier to parse in python.

import json
import subprocess

cpu_info = json.loads(subprocess.check_output("lscpu -J", shell=True))

Comments

0

For Linux, and backwards compatibility with Python (not everyone has cpuinfo), you can parse through /proc/cpuinfo directly. To get the processor speed, try:

# Take any float trailing "MHz", some whitespace, and a colon.
speeds = re.search("MHz\s*: (\d+\.?\d*)", cpuinfo_content)

Note the necessary use of \s for whitespace.../proc/cpuinfo actually has tab characters and I toiled for hours working with sed until I came up with:

sed -rn 's/cpu MHz[ \t]*: ([0-9]+\.?[0-9]*)/\1/gp' /proc/cpuinfo

I lacked the \t and it drove me mad because I either matched the whole file or nothing.

Try similar regular expressions for the other fields you need:

# Take any string after the specified field name and colon.
re.search("field name\s*: (.+)", cpuinfo_content)  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.