Module __file__ attribute in Python 3

S

Steven D'Aprano

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?
 
D

Diez B. Roggisch

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?

I'm +1 on this. Introducing __source__ shouldn't break anything, and is a
minor change as the mapping of loaded file to defining file is already
done.

Diez
 
M

MRAB

I'm +1 on this. Introducing __source__ shouldn't break anything, and is a
minor change as the mapping of loaded file to defining file is already
done.
+1. Leave __file__ as it is. Changing it seems an unnecessary break
with previous versions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top