Re: Getting the name of the file that imported current module
On Jul 5, 6:29 am, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: On Sun, 04 Jul 2010 21:05:56 +, 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 i guess he just likes to play things around, entertains his imagination, no need for practical reason for that -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting the name of the file that imported current module
On Jul 5, 4:05 am, Tobiah t...@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) -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting the name of the file that imported current module
On 04/07/2010 22:05, Tobiah wrote: foo.py: import bar bar.show_importer() output: 'foo' or 'foo.py' or 'path/to/foo' etc. Possible? Thanks, Tobiah import re re.__file__ 'C:\\Python26\\lib\\re.pyc' HTH. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting the name of the file that imported current module
On 07/04/2010 03:17 PM, Mark Lawrence wrote: On 04/07/2010 22:05, Tobiah wrote: foo.py: import bar bar.show_importer() output: 'foo' or 'foo.py' or 'path/to/foo' etc. Possible? Thanks, Tobiah import re re.__file__ '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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting the name of the file that imported current module
On Sun, 04 Jul 2010 21:05:56 +, 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting the name of the file that imported current module
On 05/07/2010 00:25, Michael Torrie wrote: On 07/04/2010 03:17 PM, Mark Lawrence wrote: On 04/07/2010 22:05, Tobiah wrote: foo.py: import bar bar.show_importer() output: 'foo' or 'foo.py' or 'path/to/foo' etc. Possible? Thanks, Tobiah import re re.__file__ '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. You're absolutely correct, thou shalt not post late at night when very tired. :) Cheers. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list