Module __file__ attribute in Python 3

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Aug 29 11:21:53 EDT 2008


On the python-dev mailing list, a question has be raised about a change 
to module.__file__ in Python 3.

http://www.gossamer-threads.com/lists/python/dev/674923#674923

In Python 2.x, m.__file__ is the name of the file that the module was 
imported from. That file might end with .py, .pyc, .pyo, or even more 
exotic extensions if Python's import mechanism has been customized.

For reasons explained here:
http://bugs.python.org/issue1762972

Python 3.0 will introduce a patch that makes m.__file__ always specify 
the source file (.py) if it exists, and only if that file doesn't exist 
will it specify the actual file used (.pyc or .pyo).

That will mean that there will be no (easy?) way to determine after the 
import whether the module was imported from a source file or a compiled 
file. I've suggested that the original justification for the patch no 
longer applies, and that the secondary purpose of the patch is better 
solved by leaving __file__ as it is in Python 2.x, and introducing a new 
attribute __source__. 

What do people think? There seems to be a little interest on the python-
dev list from a couple of people, but not enough to actually lead to any 
action. Does anyone care what m.__file__ is? Does anyone else feel that 
this issue is worth pursuing?


-- 
Steven



More information about the Python-list mailing list