Categories
Python Answers

How to compare version numbers in Python?

Spread the love

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 packaging module.

We can install it with:

pip install packaging

For instance, we write:

from packaging import version

is_less = version.parse("2.3.1") < version.parse("10.1.2")
print(is_less)

We call version.parse with the version strings.

And then we can compare the parsed version objects with the usual comparison operators.

Therefore, is_less is True.

Conclusion

To compare version numbers in Python, we can use the packaging module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *