Getting the name of the file that imported current module
kedra marbun
kedra.marbun at gmail.com
Tue Jul 6 00:25:26 EDT 2010
On Jul 5, 4:05 am, Tobiah <t... at rcsreg.com> wrote:
> foo.py:
>
> import bar
> bar.show_importer()
>
> output:
>
> 'foo' or 'foo.py' or 'path/to/foo' etc.
>
> Possible?
>
> Thanks,
>
> Tobiah
if what you mean by 'importer' is the one that really cause py to load
the mod, then why not dynamically set it?
foo.py
------
import bar, sys
if '_importer' not in bar.__dict__: bar._importer =
sys.modules[__name__]
bar.py
------
def show_importer(): return _importer
or
you could borrow space from builtins. i don't know if it breaks any
rule ;)
foo.py
------
def set_importer(mod):
bdict = (__builtins__.__dict__ if __name__ == '__main__' else
__builtins__)
if '_importer' not in bdict:
bdict['_importer'] = {mod : sys.modules[__name__]}
else:
if mod not in bdict:
bdict['_importer'][mod] = sys.modules[__name__]
import bar
set_importer(bar)
More information about the Python-list
mailing list