skip to content | skip to sidebar

pycpuid: CPUID for Python

pycpuid is a very simple Python extension. It reads the information available from the CPUID assembly instruction, and makes it available to any Python program. I needed it to decide on some codepath based on whether the box supported SSE2. In particular, I needed to know if I could import an extension module that was optimized with SSE2

import pycpuid
if pycpuid.HAS_SSE2:
    import foobar_sse2 as foobar
else:
    import foobar

I didn’t found anything alike, so I coded it myself. And here it is for the rest of you …

It is not the goal of pycpuid to provide a full report of all CPUID information available. It’s merely a way to get raw access to the machine instruction from within Python. Some functions are provided for translation to something human readable, but this is far from complete. Full details on how to interpret the raw data can be found in the application notes of Intel and AMD.

using it

There’s not much to it, really. pycpuid is just a bunch of module constants. Just import the module and access the constants. the HAS_FOOBARs are Boolean flags to indicate whether the feature is available. The function features() returns a list of all the available features as strings. There are some other functions like vendor() and brand_string() you can use to identify the CPU.

import pycpuid
print "has SSE2:", pycpuid.HAS_SSE2
print "all availabe features:", pycpuid.features()
print "brand string:", pycpuid.brand_string()

subversion repository

download source

building and installing

There’s two simple steps to it:

  1. Unpack the source code into a temporary directory, or grab it from the repository
  2. Enter setup.py install on the command line. If you’re doing this on a Windows box and things freak out because you don’t have a C++ compiler installed or properly configured, you might be interested in my five little steps how to use the Visual C++ 2005 Express compiler (which is availabe for free!)

license

pycpuid is licensed under the GNU Lesser General Public License (LGPL) so that you can import pycpuid in your Python code without it imposing any license restrictions on your code.