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

Guido van Rossum guido at python.org
Sun Feb 7 01:13:38 CET 2010

On Sat, Feb 6, 2010 at 4:04 PM, Christian Heimes <lists at cheimes.de> wrote:
> Guido van Rossum wrote:
>> What we do instead, is code in site.py that walks over the elements of
>> sys.path and turns them into absolute paths. However this code runs
>> before '' is inserted in the front of sys.path, so that the initial
>> value of sys.path is ''.
>> You may want to print the value of sys.path at various points to see
>> for yourself.
> I ran into the issue on Debian or Ubuntu (can't remember) several years
> ago. The post-install script of the Python package did something like
> "cd /usr/lib/pythonX.Y && ./compileall.py", so all pyc files were
> created relative to the library root of Python. The __file__ attribute
> of all pre-compiled Python files were relative, too.

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

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

More information about the Python-Dev mailing list