[Python-3000] A few small py3k wishes
Talin
talin at acm.org
Mon Apr 3 01:08:13 CEST 2006
Thomas Wouters <thomas <at> python.org> writes:
> I'm not sure what you're missing. The __main__ module has __file__:
> centurion:~ > cat tmp.py
> print "__file__:", __file__
> import __main__
> print "__main__.__file__:", __main__.__file__
>
> centurion:~ > python tmp.py
> __file__: tmp.py
> __main__.__file__: tmp.py
Except that they are not the same!
When I print __file__ from my __main__ module, I get the name of the
file only, no path.
However, when I print __file__ from my imported module, I get the
complete, absolute path to the module.
In fact, if I do a "print dir()" for the main module and the imported
module, I get something like this:
main: ['__builtins__', '__file__', '__name__', '__pymate', 'input', 'raw_input']
imported: ['__builtins__', '__doc__', '__file__', '__name__',
'__path__', 'fnmatch', 'os']
Note that the impored module has a "__path__" attribute, while the
main module does not.
The reason I bring this up is that it is a common usage pattern for
a particular source file to be run as both a main and as an imported
module. Suppose you have a bunch of test data that is in a subdir
of your source file. You'd like to be able to have a single, uniform
way to locate your module's data, regardless if you are __main__
or not.
Feel free to let me know if I am missing something...
-- Talin
More information about the Python-3000
mailing list