[Tutor] where am I, revisited

Jeff Shannon jeff@ccvcorp.com
Mon, 04 Mar 2002 09:40:36 -0800


Sheila King wrote:

> On Fri, 01 Mar 2002 11:01:49 -0800, "Jeff Shannon" <jeff@ccvcorp.com>
> wrote about Re: [Tutor] where am I, revisited:
>
> > I think that a better option would be to use the __file__ attribute of on of your modules.
> > This contains the (fully qualified) path of the file that the module was loaded from.
> >
> > >>> import os
> > >>> print os.__file__
> > C:\PYTHON21\lib\os.pyc
> > >>>
>
> However, I don't think your suggested fix will work for the particular
> script *I'm* running (where I got this suggestion that I made to Kirk). The
> modules from the standard Python library are in a completely different
> location, and I have no modules that I'm using in the same location as the
> script. I get the impression that
> sys.argv[0].__file__
>
> probably doesn't work? I have to do that on an imported module? But I have
> no imported module in that directory, so...

I believe (though you'll want to test this out) that an unqualified __file__ refers to the file of
the currently executing module -- in other words, it's just like any other module-global attribute,
requiring qualification when you're looking for the attribute belonging to an outside (imported)
module.  However, I haven't tested this recently, so I could be mistaken.  :)

Jeff Shannon
Technician/Programmer
Credit International