[Tutor] Querying a module's package path?
Eric Pavey
warpcat at gmail.com
Wed Nov 11 19:26:20 CET 2009
>
> Eric Pavey wrote:
>>
>>> Presume I have a package 'a' like this:
>>>
>>> * /pystuff (added to sys.path)
>>> o /a (start of my package)
>>>
>>> + __init__.py
>>> + /b # __init__.py
>>> # module.py
>>>
>>> to import module.py:
>>> import *a.b.module*
>>>
>>> What I'm trying to find is a way to query exactly what I typed above (in
>>> bold): Inside of module.py, can it query it's path back to the package
>>> root?
>>>
>>
>> Why would you want that? Can you describe the problem you're having now
>> that requires you to know the module's package path? Right now, you're just
>> describing a solution to your problem, not the problem itself. Maybe we can
>> find another solution that doesn't involve knowing the module's package
>> path.
>>
>>
> I know the feeling: "why would he ever want *that*...?" :)
>
> I'm building a high-level character rigging system in Autodesk Maya for the
> next game I'm on, which uses Python as the scripting language. Each piece
> of the animation rig (which are a collection of 'physical nodes in Maya') is
> authored via a Python module, and I want to store on the nodes which code
> created which piece. So during the execution of a module, it creates a
> variety of rigging nodes in Maya that perform their function inside that
> software. I want to embed the name of the module, relative to the path,
> into the node itself, so later unrelated code can query up what built it,
> and call to additional Python modules based on that.
>
> In a simpler sense it could be abstracted like this: I have a module that
> creates a text file. In that text file, I want to write out the name of the
> module, with package, that made it, so later I know what code created it.
>
> I could completely hand-code this info in each module, but I wanted to make
> it automated so it's easier for other people to use my code and not screw it
> up :P Plus my package is a bit in flux as I develop it, so things might
> move around, and I don't want to have to remember to go back and change a
> bunch of hard-coded paths.
>
> I have some hacky code now that does what I need, but this is screaming for
> some built-in system that returns it, IMO.
>
> thanks!
>
Found the answer, at least relative to a class, which is good enough :)
# whereAmI.py, in the data package
class Spam(object):
def __init__(self):
print self.__module__
>>> import data.whereAmI
>>> eggs = whereAmI.Spam()
data.whereAmI
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/6994c5d8/attachment.htm>
More information about the Tutor
mailing list