__file__ for relative imports

holger at trillke.net holger at trillke.net
Thu Mar 21 21:22:53 EST 2002


> There is another tiny bit in that area which might be debated as well.
> 
> `__file__' is not defined for the `__main__' module.  But `sys.argv[0]' in
> that case contains the information one might have hoped out of `__file__'.
> I would like if `__file__' was just dependably defined in all circumstances,
> the best the Python interpreter can.  If this was done, the programmer would
> then not have to resort anymore to convoluted code for finding the directory
> and file for the current module.  

also inspect.getsourcelines and friends should work inside __main__.
I mean if you define a class in an module file you can get the source via
"inspect" but not so if (the same) file is executed as __main__. 

At least i don't currently understand why this is so.
Maybe because __main__ *can* mean that you are reading from stdin 
(e.g. python prompt)? 

Maybe it's because __file__ is not defined :-)

I'd really like to know ...

	holger krekel




More information about the Python-list mailing list