how to know the importing file name from an imported file?

Dave Angel davea at
Fri Apr 17 15:09:51 CEST 2009

Visco Shaun wrote:
> Hi
> Is there a way to know the name of the script(say A), which is importing
> a module(say B), from B?
> ie in above situation i should be able to get name 'A' through some way
> in B, when A contains an 'import B' statement.

The full path to the script is stored in sys.argv[0]

Of course the script may not import B directly, so it may not have such 
an import statement.  If you're not looking for the script, but for the 
particular module that imports you, I don't know of a way. Naturally, 
it'd be a list of modules, not a single one.

Why not have the A module invoke a function in B, giving its own name?

More information about the Python-list mailing list