Python3.7及更高版本:如何确定Linux发行版?

2024-04-24 21:38:56 发布

您现在位置:Python中文网/ 问答频道 /正文

因为Python Docs清楚地表明{}是:

Deprecated since version 3.5, will be removed in version 3.7.

通过Python检测Linux发行版的正确且经得起未来考验的方法是什么?在


Tags: 方法indocsversionlinuxbewilldeprecated
3条回答

您可以使用^{} project

$ pip install distro
$ python
>>> import distro
>>> distro.linux_distribution(full_distribution_name=False)
('centos', '7.1.1503', 'Core')

这个项目来自issue #1322,这导致了函数的弃用。从project README

It is a renewed alternative implementation for Python's original platform.linux_distribution function, but it also provides much more functionality which isn't necessarily Python bound like a command-line interface

该方法已从platform库中删除,因为用于检测您所使用的发行版的正确方法的更改速度快于Python发布计划。从上面的错误报告:

The stdlib is not the right place for things that change this often. Just look at how many semi standards we've seen in the last few years. There's no point in trying to follow these in a slow moving code base as the Python stdlib. It's much better to put the functionality into a PyPI module which can be updated much more frequently.

根据Jim的回答,这个功能将从Python中删除。distro包似乎是推荐的替代方案:

$ pip3 install  user distro

$ python3
Python 3.6.3 (default, Oct  9 2017, 12:07:10) 
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import distro
>>> distro.linux_distribution()
('Fedora', '27', 'Twenty Seven')

那就剩下一个包裹了。来自此更改的新增内容:

The platform.dist() and platform.linux_distribution() functions are now deprecated. Linux distributions use too many different ways of describing themselves, so the functionality is left to a package. (Contributed by Vajrasky Kok and Berker Peksag in bpo-1322.)

您可以查看删除它的issue 1322以进行更详细的讨论,还有a package there already。在

由于Python标准库会带来维护开销,因此您无法在Python标准库中执行此操作。在

相关问题 更多 >