Menu Close

How to compare version numbers in Python?

Sometimes, we want to compare version numbers in Python.

In this article, we’ll look at how to compare version numbers in Python.

How to compare version numbers in Python?

To compare version numbers in Python, we can use the version.parse method to parse the version string into an object and compare that.

For instance, we write

from packaging import version

greater = version.parse("2.3.1") < version.parse("10.1.2")

to call version.parse with the version string.

And then we use < compare the 2 version objects.

Conclusion

To compare version numbers in Python, we can use the version.parse method to parse the version string into an object and compare that.

Posted in Python, Python Answers