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.
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'
withsys.platform.startswith('linux')
, or directlysys.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With