Search code examples
pythonegg

Robust detection of module name of Python egg


Lets say I make an egg

~/toolz$ python setup.py bdist_egg
creating 'dist/toolz-0.7.4-py3.4.egg'

How do I detect the module name of this egg? Will it always be the word preceding the version string?

>>> egg_name = 'toolz-0.7.4-py3.4.egg'
>>> egg_name.split('-')[0]
'toolz'

Or does this fail sometimes? Is there a library I should use for this instead?


Solution

  • In [1]: import pkg_resources
    
    In [2]: pkgs = list(pkg_resources.find_distributions('dist/toolz-0.7.4-py2.7.egg'))
    
    In [3]: assert len(pkgs) == 1
    
    In [4]: pkg = pkgs[0]
    
    In [5]: pkg.project_name
    Out[5]: 'toolz'