0
# Version Components
1
2
Functions for parsing and accessing individual components of version numbers. These functions break down version strings into major, minor, and build number components for programmatic access.
3
4
## Capabilities
5
6
### Version Parts
7
8
Returns version as a tuple of individual components.
9
10
```python { .api }
11
def version_parts(best: bool = False) -> Tuple[str, str, str]:
12
"""
13
Return version as tuple of (major, minor, build_number).
14
15
Args:
16
best: If True, use the most precise version from all data sources
17
18
Returns:
19
Tuple[str, str, str]: (major, minor, build_number)
20
21
Examples:
22
("20", "04", "") for "20.04"
23
("8", "4", "2105") for "8.4.2105"
24
("", "", "") if version cannot be parsed
25
"""
26
```
27
28
Usage examples:
29
```python
30
import distro
31
32
# Basic version parts
33
major, minor, build = distro.version_parts()
34
print(f"Version: {major}.{minor}.{build}") # "20.04."
35
36
# Using best available precision
37
major, minor, build = distro.version_parts(best=True)
38
if build:
39
print(f"Full version: {major}.{minor}.{build}") # "8.4.2105"
40
else:
41
print(f"Version: {major}.{minor}") # "20.04"
42
```
43
44
### Major Version
45
46
Returns the major version number component.
47
48
```python { .api }
49
def major_version(best: bool = False) -> str:
50
"""
51
Return the major version number.
52
53
Args:
54
best: If True, use the most precise version from all data sources
55
56
Returns:
57
str: Major version number as string, empty if not available
58
59
Examples:
60
"20" for Ubuntu 20.04
61
"8" for CentOS 8.4
62
"11" for Debian 11
63
"""
64
```
65
66
Usage example:
67
```python
68
import distro
69
70
major = distro.major_version()
71
print(f"Major version: {major}") # e.g., "20"
72
73
# Using best precision
74
major_best = distro.major_version(best=True)
75
print(f"Major (best): {major_best}")
76
```
77
78
### Minor Version
79
80
Returns the minor version number component.
81
82
```python { .api }
83
def minor_version(best: bool = False) -> str:
84
"""
85
Return the minor version number.
86
87
Args:
88
best: If True, use the most precise version from all data sources
89
90
Returns:
91
str: Minor version number as string, empty if not available
92
93
Examples:
94
"04" for Ubuntu 20.04
95
"4" for CentOS 8.4
96
"0" for Debian 11.0
97
"""
98
```
99
100
Usage example:
101
```python
102
import distro
103
104
minor = distro.minor_version()
105
print(f"Minor version: {minor}") # e.g., "04"
106
107
# Check if minor version exists
108
if minor:
109
print(f"Full version: {distro.major_version()}.{minor}")
110
else:
111
print(f"Version: {distro.major_version()}")
112
```
113
114
### Build Number
115
116
Returns the build number or patch level component.
117
118
```python { .api }
119
def build_number(best: bool = False) -> str:
120
"""
121
Return the build number or patch level.
122
123
Args:
124
best: If True, use the most precise version from all data sources
125
126
Returns:
127
str: Build number as string, empty if not available
128
129
Examples:
130
"2105" for CentOS 8.4.2105
131
"3" for Ubuntu 20.04.3
132
"" for versions without build numbers
133
"""
134
```
135
136
Usage examples:
137
```python
138
import distro
139
140
build = distro.build_number()
141
if build:
142
print(f"Build number: {build}") # e.g., "2105"
143
else:
144
print("No build number available")
145
146
# Construct full version string
147
major = distro.major_version()
148
minor = distro.minor_version()
149
build = distro.build_number()
150
151
version_str = major
152
if minor:
153
version_str += f".{minor}"
154
if build:
155
version_str += f".{build}"
156
157
print(f"Constructed version: {version_str}") # e.g., "8.4.2105"
158
```
159
160
## Best vs Default Behavior
161
162
The `best` parameter controls which data source is used for version information:
163
164
- **Default (`best=False`)**: Uses the first available version from the primary data source (usually os-release)
165
- **Best (`best=True`)**: Searches all data sources and returns the most detailed/precise version available
166
167
```python
168
import distro
169
170
# Compare default vs best precision
171
default_version = distro.version()
172
best_version = distro.version(best=True)
173
174
print(f"Default: {default_version}") # "8.4"
175
print(f"Best: {best_version}") # "8.4.2105"
176
177
# Version parts comparison
178
default_parts = distro.version_parts()
179
best_parts = distro.version_parts(best=True)
180
181
print(f"Default parts: {default_parts}") # ("8", "4", "")
182
print(f"Best parts: {best_parts}") # ("8", "4", "2105")
183
```
184
185
## Data Source Priority
186
187
Version components are extracted from data sources in this priority order:
188
189
1. **os-release** file (`/etc/os-release` or `/usr/lib/os-release`)
190
2. **lsb_release** command output
191
3. **Distro release files** (e.g., `/etc/redhat-release`, `/etc/debian_version`)
192
4. **uname** command (for BSD systems)
193
194
When `best=True`, all sources are checked and the most complete version information is returned.