Getting the name of the file that imported current module

T

Tobiah

foo.py:

import bar
bar.show_importer()

output:

'foo' or 'foo.py' or 'path/to/foo' etc.

Possible?

Thanks,

Tobiah
 
M

Mark Lawrence

foo.py:

import bar
bar.show_importer()

output:

'foo' or 'foo.py' or 'path/to/foo' etc.

Possible?

Thanks,

Tobiah
'C:\\Python26\\lib\\re.pyc'

HTH.

Mark Lawrence.
 
M

Michael Torrie

'C:\\Python26\\lib\\re.pyc'

I think this is exactly opposite of what he was asking for.

Given than any number of modules can import other modules but each
module really only exists once in the Python object space (normally)
would mean that what the OP is asking for isn't possible.
 
S

Steven D'Aprano

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?
 
M

Mark Lawrence

I think this is exactly opposite of what he was asking for.

Given than any number of modules can import other modules but each
module really only exists once in the Python object space (normally)
would mean that what the OP is asking for isn't possible.

You're absolutely correct, thou shalt not post late at night when very
tired. :)

Cheers.

Mark Lawrence
 
K

kedra marbun

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?

i guess he just likes to play things around, entertains his
imagination, no need for practical reason for that
 
K

kedra marbun

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top