12

The book I'm reading shows me sys.platform in python3, on windows this runs fine, but in Linux I keep getting back "Linux2" even though my kernel is Linux3.

Doing a search on StackOverflow I have seen people mention platform.system and os.name. The first of these will tell you Linux/Windows but not what version, and the later gives you the technology name as in NT/Posix.

I'm a bit confused to which is the most accurate and preferred method used by experience python coders. Is it possible to obtain "windows7" or Linux3?

Many thanks. ps. Still new to stackoverflow, hopefully formatted my question correctly.

4
  • 1
    Just came across the python bug report which states python3.3 resolves this by making sys.platform now report only "Linux" and not "Linux2" or "Linux3" etc. bugs.python.org/issue12326 I am still interested to know what experienced coders prefer to use though please. Commented May 2, 2012 at 14:43
  • Just out of curiosity, what distro are you using that you have the Linux 3 kernel? Commented May 2, 2012 at 14:44
  • 1
    I should have guessed. Arch is always the distro that smokes out problems early for the rest of us :-) Commented May 2, 2012 at 15:04
  • 2
    @HankGay: Most recent releases will have Linux 3. It's not actually a major change, Linus just decided it was time for a new number. Commented May 2, 2012 at 16:17

2 Answers 2

19

You shouldn't rely on the number, because, as you pointed out, it is inconsistent. It actually shows the major version of the kernel on the system where Python was compiled.

Moreover, it has been removed in Python 3.3:

issue 12326: On Linux, sys.platform doesn't contain the major version anymore. It is now always 'linux', instead of 'linux2' or 'linux3' depending on the Linux version used to build Python. Replace sys.platform == 'linux2' with sys.platform.startswith('linux'), or directly sys.platform == 'linux' if you don't need to support older Python versions.
What's New In Python 3.3 » Porting Python code

See also: sys.platform

So the preferred way to find out if the system is Linux is sys.platform.startswith('linux'). It works for all versions of Python and Linux.

There doesn't seem to be a way to find out the operating system name more precisely in the standard library.

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

1 Comment

Indeed. The Kernel's major version number is irrelevant and never should've been in sys.platform in the first place. platform.uname() contains the specific versions if you need them.
0

check the documentation here, it both explains why 'linux2' as well as how to treat it in a backwards compatible manner:

https://docs.python.org/2/library/sys.html

Changed in version 2.7.3: Since lots of code check for sys.platform == 'linux2', and there is no essential change between Linux 2.x and 3.x, sys.platform is always set to 'linux2', even on Linux 3.x. In Python 3.3 and later, the value will always be set to 'linux', so it is recommended to always use the startswith ...

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.