__file__ for relative imports

Jeff Shannon jeff at ccvcorp.com
Fri Mar 22 13:49:29 EST 2002


François Pinard wrote:

> [Ken Seehof]
>
> > > Robin Becker <robin at jessikat.fsnet.co.uk> writes:
> > >
> > > > Is there an obvious reason why modules imported from the current
> > > > directory don't have an absolute pathname for __file__?
>
> > I keep bumping into that one too.  [...]
>
> 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.  ....

I like this idea a lot.  Even if, for __main__, we ended up with __file__ being
(potentially) '<standard input>', or '<from string>', or even None, it'd be nice
to be able to depend on the name being defined.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list