Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Python's __file__ actually do? [duplicate]

Tags:

python

I've always assumed that the __file__ variable always gave you the current file name, but that doesn't seem to be exactly what it does. Since I've been facing a bug if I assume this to be true.

Someone told me "that __file__ refers to the last module searched" And this seems to be more accurate, but I'd like to know what __file__ is really supposed to do.

I couldn't find anything concrete mentioned in the Python docs. A lot places seem to mention it, but aren't very clear about it.

http://docs.python.org/2/c-api/import.html?highlight=__file__

http://docs.python.org/2/c-api/module.html?highlight=__file__

like image 871
ffledgling Avatar asked Oct 03 '22 15:10

ffledgling


1 Answers

 __file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute is not present
 for C modules that are statically linked into the interpreter; for
 extension modules loaded dynamically from a shared library, it is the
 pathname of the shared library file.

from here: http://mail.python.org/pipermail/python-dev/2010-February/097461.html

like image 61
AngelloMaggio Avatar answered Oct 07 '22 18:10

AngelloMaggio