Getting the name of the file that imported current module

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Jul 4 19:29:05 EDT 2010


On Sun, 04 Jul 2010 21:05:56 +0000, Tobiah wrote:

> foo.py:
> 
> import bar
> bar.show_importer()
> 
> output:
> 
> 'foo' or 'foo.py' or 'path/to/foo' etc.
> 
> Possible?

I don't think so. Your question isn't even well-defined. Given three 
modules:

# a.py
import b
import d

# b.py
import d

# c.py
import a
import d
import b
print d.show_importer()

and you run c.py, what do you expect d.show_importer() to return?

And what about "from d import show_importer" -- does that count as 
"importing d"?

Why do you think that a module needs to know what other modules imported 
it? I can't imagine why this would be necessary, what are you intending 
to do with it?



-- 
Steven



More information about the Python-list mailing list