gjs
Posts: 148
Joined: Fri Dec 02, 2011 8:04 pm

Detect if running on RPi

Sun Sep 01, 2013 1:56 am

From my python program, how can I determine if the program is running on a Raspberry Pi instead of, for example, a Beaglebone?

Thanks.

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: Detect if running on RPi

Sun Sep 01, 2013 2:00 am

I check the output of:

Code: Select all

$ uname -m
to at least confirm the processor version. Not a definite that it's a Pi, but certainly excludes most non-Pi hardware.

User avatar
rpdom
Posts: 13564
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Detect if running on RPi

Sun Sep 01, 2013 3:11 am

You could check the contents of /proc/cpuinfo. There's a few values in there that would probably identify a Pi. I can't check right now, as my Pi is switched off and I don't have a Beglebone to compare it with.

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Detect if running on RPi

Sun Sep 01, 2013 8:34 am

If it is an RPi with a built-in ethernet port (ie. won't work with a Model A with no LAN port) then the eth0 HWaddr field returned by ifconfig will probably start with b8:27:eb:xx:xx:xx if it is a device produced by the Raspberry Pi Foundation.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Detect if running on RPi

Sun Sep 01, 2013 8:43 am

As with most such things, it depends on how rigorous and/or future-proof you need to be, but, for now anyway, it looks like:

grep Hardware /proc/cpuinfo

is pretty good. On the Pi, this gives:

Hardware : BCM2708

Needless to say, on non-Broadcom-based-boards, this will give something utterly different.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

gjs
Posts: 148
Joined: Fri Dec 02, 2011 8:04 pm

Re: Detect if running on RPi

Sun Sep 01, 2013 9:24 am

Thanks for your replies.

The uname command is probably the least definate as it returns unknown for the processor and hardware-platform items. Using the MAC address won't work on a model A, as stated. Grepping cpuinfo seems the best option as it returns the SOC name.

However, all of these rely on OS commands which, presumably, means they are OS dependent. Is there a Python command or technique that can be used regardless of the presence of a particular OS utility?

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Detect if running on RPi

Sun Sep 01, 2013 9:34 am

gjs wrote:However, all of these rely on OS commands which, presumably, means they are OS dependent. Is there a Python command or technique that can be used regardless of the presence of a particular OS utility?
You could use sys.platform to find out which OS you're running on then use the relevant command for a particular system.

Dave.
Apple say... Monkey do !!

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Detect if running on RPi

Mon Sep 02, 2013 4:01 pm

gjs wrote:However, all of these rely on OS commands which, presumably, means they are OS dependent. Is there a Python command or technique that can be used regardless of the presence of a particular OS utility?
Simply open /proc/cpuinfo directly in Python, read it a line at a time, and do the "grepping" yourself with http://docs.python.org/2/library/stdtyp ... startswith

gjs
Posts: 148
Joined: Fri Dec 02, 2011 8:04 pm

Re: Detect if running on RPi

Tue Sep 03, 2013 12:57 pm

Do all Linux distributions have /proc/cpuinfo ?

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Detect if running on RPi

Tue Sep 03, 2013 1:51 pm

gjs wrote:Do all Linux distributions have /proc/cpuinfo ?
Yup, /proc (and /sys) is a "virtual filesystem" which doesn't actually exist on disk (even though to all intents and purposes they look like regular files), but which gets created dynamically by the Linux kernel.

gjs
Posts: 148
Joined: Fri Dec 02, 2011 8:04 pm

Re: Detect if running on RPi

Tue Sep 03, 2013 4:05 pm

Great, thanks.

User avatar
paddyg
Posts: 2279
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Detect if running on RPi

Tue Sep 03, 2013 6:07 pm

don't hack it, use platform as @davef says. It's platform independent python.

in pi3d we decided it was actually more relevant to know what shared libraries were available rather than the actual machine or operating system. We do this with ctypes.util.find_library() which seems to work well.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Detect if running on RPi

Tue Sep 03, 2013 6:23 pm

paddyg wrote:don't hack it, use platform as @davef says. It's platform independent python.

in pi3d we decided it was actually more relevant to know what shared libraries were available rather than the actual machine or operating system. We do this with ctypes.util.find_library() which seems to work well.
Yeah, I guess the pertinent question for the OP would be why does he need to know whether or not he's running on a Pi, when Python makes writing portable cross-platform code really easy :)

Tom Swirly
Posts: 168
Joined: Tue Sep 25, 2012 1:08 am

Re: Detect if running on RPi

Sun Mar 16, 2014 8:36 pm

Coming into this late...

`platform` is definitely not good enough if you're writing real cross-platform code.

The example that led me to this discussion was wanting to know if I could use RPi.GPIO - but the best that you can get from `platform` is:

Code: Select all

IS_DEBIAN = platform.linux_distribution()[0].lower() == 'debian' 
- which allows you to tell if the platform is Debian but not specifically if it's an RPi.

Of course, you could just try to import RPi.GPIO and report the failure if it didn't work, but I want my error messages to clearly distinguish between "not an RPi" and "is an RPi but your distribution doesn't have RPi.GPIO".

If you want to see what I did for this in echomesh, the code is here (and is open source if you want to take it...) - and I basically do the /proc/cpuinfo grep as suggested above.

Return to “Python”