The Python Oracle

How do I check the operating system in Python?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Luau

--

Chapters
00:00 Question
00:30 Accepted answer (Score 492)
00:59 Answer 2 (Score 50)
01:22 Answer 3 (Score 16)
01:59 Answer 4 (Score 7)
02:13 Thank you

--

Full question
https://stackoverflow.com/questions/8220...

Accepted answer links:
[sys.platform]: https://docs.python.org/library/sys.html...
[the documentation]: https://docs.python.org/library/sys.html...
[“What OS am I running on?”]: https://stackoverflow.com/a/1857/1513933

Answer 2 links:
[platform.system]: https://docs.python.org/3/library/platfo...

Answer 3 links:
[sys.platform]: http://docs.python.org/library/sys.html#...
[os.uname()]: http://docs.python.org/library/os.html#o...
[Python System Information]: https://bitbucket.org/chrismiles/psi/wik...
[pywin32]: http://sourceforge.net/projects/pywin32/
[psutil]: https://pypi.org/project/psutil/

Answer 4 links:
[platform]: http://docs.python.org/library/platform....

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #linux #operatingsystem

#avk47



ACCEPTED ANSWER

Score 542


You can use sys.platform:

from sys import platform
if platform == "linux" or platform == "linux2":
    # linux
elif platform == "darwin":
    # OS X
elif platform == "win32":
    # Windows...

sys.platform has finer granularity than sys.name.

For the valid values, consult the documentation.

See also the answer to “What OS am I running on?”




ANSWER 2

Score 16


You can get a pretty coarse idea of the OS you're using by checking sys.platform.

Once you have that information you can use it to determine if calling something like os.uname() is appropriate to gather more specific information. You could also use something like Python System Information on unix-like OSes, or pywin32 for Windows.

There's also psutil if you want to do more in-depth inspection without wanting to care about the OS.




ANSWER 3

Score 7


More detailed information are available in the platform module.




ANSWER 4

Score 5


You can use sys.platform.