From: Tobiah on
foo.py:

import bar
bar.show_importer()

output:

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

Possible?

Thanks,

Tobiah
From: Mark Lawrence on
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.

From: Michael Torrie on
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.
From: Steven D'Aprano on
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
From: Mark Lawrence on
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