how to get the path of a module (myself) ?

Stef Mientki stef.mientki at gmail.com
Mon Jun 1 19:46:37 EDT 2009


Thanks David,
but ....

David Lyon wrote:
> On Mon, 01 Jun 2009 23:28:16 +0200, Stef Mientki <stef.mientki at gmail.com>
> wrote:
>   
>> hello,
>>
>> I've pictures stored in a path relative to my python source code.
>> To get a picture, I need to know what path I'm on in each python module.
>> I thought __file__ would do the job,
>> but apparently I didn't read the documentation carefully enough,
>> because file is the path to the module that called my module.
>>
>> Any ways to get the path of "myself" ?
>>     
>
> This ain't the official way... but the hackers way.....
>
> Check site.path (import site)...
>   
always return None in my case
> If your module got loaded, and it's own succinct directory or .egg, then
> it will have been added to site.path. 
>
> You might have to parse the values in site.path but we're only talking
> a few lines of code because you already know the package name.
>
> If not, there's another way through pkg_utils...
>   
I don't seem to have pkg_utils,
only pkgutil, which doesn't have an apropiate function.

But I found another way, don't know if that's reliable:
import inspect
print inspect.currentframe().f_code.co_filename

cheers,
Stef
> Regards
>
> David
>
>   




More information about the Python-list mailing list