Having a module ask itself where it's at.

Andrew Bennetts andrew-pythonlist at puzzling.org
Sat Feb 15 20:23:26 EST 2003


On Sat, Feb 15, 2003 at 01:27:26PM -0800, logistix wrote:
> I'm trying to bundle a ConfigParser file with a package I'm working
> on.  How can I have the module in the package locate this file?
> 
> Since it's being loaded via import, and directory could really be the
> current working directory when the script is executed.

If your package is called "mypackage":
    import os.path
    import mypackage
    
    path = os.path.dirname(mypackage.__file__)
    path = os.path.join(path, 'config.ini')
    
(Completely untested)

-Andrew.






More information about the Python-list mailing list