Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sys.platform = Linux2 but not Linux3?

Tags:

python-3.x

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.

like image 356
Zenettii Avatar asked May 02 '12 14:05

Zenettii


Video Answer


1 Answers

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.

like image 134
Oleh Prypin Avatar answered Oct 10 '22 09:10

Oleh Prypin