<br><div><span class="gmail_quote">On 4/3/06, <b class="gmail_sendername">Talin</b> &lt;<a href="mailto:talin@acm.org">talin@acm.org</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Thomas Wouters &lt;thomas &lt;at&gt; <a href="http://python.org">python.org</a>&gt; writes:<br><br>&gt; I'm not sure what you're missing. The __main__ module has __file__:<br><br>Except that they are not the same!<br><br>
When I print __file__ from my __main__ module, I get the name of the<br>file only, no path.</blockquote><div><br>You get the path that was used to run the file:<br><br>centurion:~ &gt; python ~thomas/tmp.py<br>__file__: /home/thomas/tmp.py
<br>__main__.__file__: /home/thomas/tmp.py<br>&nbsp;</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">However, when I print __file__ from my imported module, I get the
<br>complete, absolute path to the module.</blockquote><div><br>Incorrect: you get the path that was used to import the module. If a relative path is in sys.path (like its first element usually is), the module's __file__ will be a relative path:
<br><br>&nbsp;centurion:~ &gt; python -c 'import tmp'<br>__file__: tmp.py<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">In fact, if I do a &quot;print dir()&quot; for the main module and the imported
<br>module, I get something like this:<br><br>main: ['__builtins__', '__file__', '__name__', '__pymate', 'input', 'raw_input']<br>imported: ['__builtins__', '__doc__', '__file__', '__name__',<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '__path__', 'fnmatch', 'os']
<br><br>Note that the impored module has a &quot;__path__&quot; attribute, while the<br>main module does not.</blockquote><div><br>Only packages have a __path__ attribute. Try checking on a module that is a .py file. Likewise, not all modules have a __file__ attribute:
<br><br>&gt;&gt;&gt; posix.__file__<br>Traceback (most recent call last):<br>&nbsp; File &quot;&lt;stdin&gt;&quot;, line 1, in ?<br>AttributeError: 'module' object has no attribute '__file__'<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
The reason I bring this up is that it is a common usage pattern for<br>a particular source file to be run as both a main and as an imported<br>module. Suppose you have a bunch of test data that is in a subdir<br>of your source file. You'd like to be able to have a single, uniform
<br>way to locate your module's data, regardless if you are __main__<br>or not.</blockquote><div><br>__file__ is it. os.path.abspath() it with os.getcwd().<br></div></div><br>-- <br>Thomas Wouters &lt;<a href="mailto:thomas@python.org">
thomas@python.org</a>&gt;<br><br>Hi! I'm a .signature virus! copy me into your .signature file to help me spread!