[Distutils] Getting source from __file__ in egg?

skip at pobox.com skip at pobox.com
Wed Sep 10 13:13:38 CEST 2008


If I have a package installed as a zip file egg and ask for its __file__ I
might get a reference which looks like a normal file system path but isn't:

    % python
    Python 2.4.5 (#4, Apr 12 2008, 09:09:16) 
    [GCC 3.4.1] on sunos5
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import magnitude
    >>> magnitude.__file__
    '/opt/app/g++lib6/python-2.4/lib/python2.4/site-packages/magnitude-0.9.3-py2.4.egg/magnitude.pyc'
    >>> magnitude.__file__[:-1]
    '/opt/app/g++lib6/python-2.4/lib/python2.4/site-packages/magnitude-0.9.3-py2.4.egg/magnitude.py'
    >>> import os
    >>> os.path.exists(magnitude.__file__[:-1])
    False

How do I actually lay my hands on the .py file source for the magnitude
module in this case?  Do I have to recognize somehow that
.../magnitude-0.9.3-py2.4.egg is a zip archive and resort to extracting the
Python source with the zipfile module?

Thx,

Skip Montanaro


More information about the Distutils-SIG mailing list