3

I have some code that's extremely slow, I've traced it back to the use of pkg_resources.get_distribution, I'm doing this:

# ...
pkg_resources.get_distribution(module_name).version

Is there an alternative library/method I can use that does the same thing? 

Morgoth
  • 4,935
  • 8
  • 40
  • 66

1 Answers1

0

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

Morgoth
  • 4,935
  • 8
  • 40
  • 66
  • I guess what I don't quite understand is what `pkg_resources.get_distribution` is actually doing? Is it looking at something other than `module.__version__`? – KX68C4qxM51mtdzex7O8iMPU Mar 15 '17 at 18:01
  • No it doesn't, have a look over here: [pkg_resources.py](http://svn.python.org/projects/sandbox/branches/setuptools-0.6/pkg_resources.py) – Morgoth Mar 16 '17 at 07:02
  • @KX68C4qxM51mtdzex7O8iMPU, you should accept an answer if it helps you. – Morgoth Mar 18 '17 at 16:28
  • Thanks, but I'm not sure that using `module.__version__` is a 100% like-for-like replacement for my code, as I only have a list of module names as strings and they may not have been imported – KX68C4qxM51mtdzex7O8iMPU Mar 21 '17 at 11:55