Finding path to __init__.py in package

Lulu of the Lotus-Eaters mertz at gnosis.cx
Thu Feb 14 20:56:47 CET 2002


I think this is one where the answer will be so stunningly obvious that
I'll know myself on the head.  But it is not coming to me right away (I
haven't worked with packages much, mostly modules, so that might be the
problem).

Anyway, I have created a package that has various subpackages.  One
subpackage (and more to come) is documentation files.  These ASCII
documents can be read in a plain text editor, but I also want to import
their content into some namespaces.  For example, I'd like a script to
be able to include:

    from gnosis.xml.pickle.doc import history

Specifically, some other namespaces want to import some names this way.

My current setup works, but looks kludged:

    $PYTHONPATH/gnosis/xml/pickle/doc/__init__.py:
    --------------------------------------------------------------
    from os import sep
    import gnosis
    gdir = gnosis.__file__.split(sep)[:-1]
    ddir = sep.join(gdir+['xml','pickle','doc',''])
    _= lambda f: open(ddir+f).read()
    l= lambda f: _(f).rstrip().split('\n')

    howto     = _('HOWTO')
    copyright = _('COPYRIGHT')
    history   = _('HISTORY')
    security  = _('SECURITY')
    todo      = _('TODO')
    version   = _('VERSION')
    author    = l('AUTHOR')
    thanks_to = l('THANKS_TO')

That is, I *know* that each (ALLCAPS) documentation file is contained in
the same directory as the __init__.py file.  But os.getcwd() just reports
the directory I am working from, and sys.path[0] reports the directory
of the script that -imports- gnosis.xml.pickle.doc, not the directory of
the __init__.py.

What I would like is a function that asks "what directory is this
current __init__.py script running from?"

Yours, Lulu...

--
mertz@  | The specter of free information is haunting the `Net!  All the
gnosis  | powers of IP- and crypto-tyranny have entered into an unholy
.cx     | alliance...ideas have nothing to lose but their chains.  Unite
        | against "intellectual property" and anti-privacy regimes!
-------------------------------------------------------------------------





More information about the Python-list mailing list