Jean Brouwers wrote:
Assuming the module is loaded from a directory,
Which is actually an assumption that you often don't want to make. If you are able to make the assumption, the __file__ module attribute is normally fine for this kind of use.
<promotion style=shameless>
If not, and you need a general solution, rather than one that each application needs to know about, ResourcePackage is designed explicitly to solve this problem (embedding resources in packages intended to be used in large number of applications where the known environment will include simple installation of the package, py2exe, McMillan Installer etceteras).
ResourcePackage is, in essence, just an automated version of the img2py script from wxPython. It scans a directory for changed resource files (non-Python files) on import and runs the equivalent of img2py for each found resource. You simply save an updated version of the resource file to the directory and then run the app to get the new version of the resource available (the first import of the resource-package does the updating, so all new resources are updated at that time).
To use your resources, you simple import them like so:
from myreusablelibrary.resources import someicon_png
data = someicon_png.data
That works for any data-type which can be loaded from an in-memory string (notable exception being .wav files at the moment due to a bug in current wxPython); it won't work for data-types which are only loadable via filename interfaces.
http://resourcepackage.sf.net/
</promotion>
BTW, there's an alternate reading of the question which is not about resources to distribute with the app, but how to determine where the program should store user-preferences or the like while it's running. Normally that is platform specific, with the Windows version using one of the registered shell locations and Unix-ish systems using a .library directory in the user's home directory. You should *not* generally store such information in any path which is readily constructed as a relative transform from the module's __file__. There's some basic code for this kind of thing in OpenGLContext.
HTH,
Mike
...
I have a related question: I have a module that is called by several
different programs. I want that module to save data related to itself
in its own directory, so there are not multiple copies for each of the
calling programs. Is there a way to get the path of that module when
another program calls it (other than hard-coding)?
...
···
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
blog: http://zope.vex.net/~mcfletch/plumbing/