[Python-Dev] __file__ is not always an absolute path

Guido van Rossum guido at python.org
Sun Feb 7 01:42:09 CET 2010

On Sat, Feb 6, 2010 at 4:36 PM, Christian Heimes <lists at cheimes.de> wrote:
> Guido van Rossum schrieb:
>> Are you sure you remember this right? The <code>.co_filename
>> attributes will be unmarshalled straight from the bytecode file which
>> indeed will have the relative path in this case (hopefully we'll
>> finally fix this in 3.2 and 2.7). But if I read the code in import.c
>> correctly, __file__ is set on the basis of the path of the file read,
>> which in turn comes from sys.path which will have been "absolufied" by
>> site.py. Or maybe this was so long ago that site.py didn't yet do
>> that?
> I ran into the problem years ago. I can recall the Python version but it
> must have been 2.2 or 2.3, maybe 2.1. I'm not entirely sure how it
> happened, too. All I can remember that I traced the cause down to the
> way compileall was called. I've tried to reproduce the issue with Python
> 2.6 but failed. It looks like the code does the right thing.

Hm. The timing doesn't match. From the svn logs for site.py looks like
this was introduced in r17768 on 2000-09-28, which puts it before 2.0
was released.

--Guido van Rossum (python.org/~guido)

More information about the Python-Dev mailing list