It depends on the module you are working with, most python libraries implement the __version__
attribute, try that:
My timings also suggest it is the best. I tried with three different methods:
import time
import pip
import pkg_resources
from pip.operations import freeze
# __version__
t0 = time.time()
version_number = pip.__version__
t1 = time.time()
total = t1-t0
print("__version__ =", version_number, "in", total)
# pip freeze
t0 = time.time()
x = freeze.freeze()
for p in x:
if p[:p.find('==')] == "pip":
version_number = p[p.find('==')+2:]
break
t1 = time.time()
total = t1-t0
print("pip freeze =", version_number, "in", total)
# pkg_resources
t0 = time.time()
version_number = pkg_resources.get_distribution("pip").version
t1 = time.time()
total = t1-t0
print("pkg_resources =", version_number, "in", total)
Keep in mind that the pip freeze
method will be highly variable in timing, depending on how many modules you have in your system.
Results:
__version__ = 9.0.1 in 2.1457672119140625e-06
pip freeze = 9.0.1 in 0.04388904571533203
pkg_resources = 9.0.1 in 0.0016148090362548828
On my system __version__
was 7.5 times faster than pkg_resources