23

My application is assumed to be running on a Mac OS X system. However, what I need to do is figure out what version of Mac OS (or Darwin) it is running on, preferably as a number. For instance,

  • "10.4.11" would return either 10.4 or 8
  • "10.5.4" would return 10.5 or 9
  • "10.6" would return 10.6 or 10

I found out that you could do this, which returns "8.11.0" on my system:

import os
os.system("uname -r")

Is there a cleaner way to do this, or at least a way to pull the first number from the result? Thanks!

5 Answers 5

50
>>> import platform
>>> platform.mac_ver()
('10.5.8', ('', '', ''), 'i386')

As you see, the first item of the tuple mac_ver returns is a string, not a number (hard to make '10.5.8' into a number!-), but it's pretty easy to manipulate the 10.x.y string into the kind of numbers you want. For example,

>>> v, _, _ = platform.mac_ver()
>>> v = float('.'.join(v.split('.')[:2]))
>>> print v
10.5

If you prefer the Darwin kernel version rather than the MacOSX version, that's also easy to access -- use the similarly-formatted string that's the third item of the tuple returned by platform.uname().

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, this works on Debian (and probably other Linux) as well, giving false positives if you use this function to check if the system is Mac OS X
@RobertMoore platform.system() should be used to detect the underlying OS rather than directly relying on mac_ver()
This does not work for Big Sur. mac_ver()[0] returns 10.16, while I'm on 11.6.7. Of course, there is no 10.16 version, but 10.16 = 11 and no minor release info is provided. However, mac_ver()[0] does appear to work on Monterey, returning 12.4 on that system.
4

If you are already using os, you might want to use os.uname()

import os
os.uname()

1 Comment

This gives the release version of Darwin itself -- for instance, Catalina is 19.6.0--it might be helpful in some contexts, but original poster is looking for the macOS version like 10.9
1

You could parse the output of the /usr/bin/sw_vers command.

2 Comments

The uname command will give him the version - his problem is in how to get this value into a variable.
Well, at least it gives the kernel version - but he seems to have an overall problem of being able to read command output into a variable.
1

platform.mac_ver() will return a tuple (release, versioninfo, machine

So get mac version by code

>>> platform.mac_ver()[0]

'10.8.4'

this method is easy

Comments

0

If you want to run a command - like 'uname' - and get the results as a string, use the subprocess module.

import subprocess
output = subprocess.Popen(["uname", "-r"], stdout=subprocess.PIPE).communicate()[0]

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.