Library and real path name

Fredrik Lundh fredrik at
Tue Aug 23 12:25:23 CEST 2005

Michele Petrazzo wrote:
> I want to redistribute the library that I create.
> I create a project, its that when launched copy all files into
> the "site-packages/library" directory. And here it's all ok.
> When I call my library with:
> import library
> library.class()
> I want that my library know where are its real path
> (site-packages/library/)

you can use the __file__ or __path__ variables to determine this.

    # get this module's directory
    import os
    prefix = os.path.dirname(__file__)

or, inside a package file:

    # get package's main directory
    prefix = __path__[0]


More information about the Python-list mailing list