Module __file__ attribute in Python 3

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven D'Aprano

    Module __file__ attribute in Python 3

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

    Carbon60 is Canada’s preeminent cloud adoption, migration and managed cloud services provider. AWS, Azure, Google Cloud, VMWare, SOC2 and PCI compliance.


    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:


    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
  • Diez B. Roggisch

    #2
    Re: Module __file__ attribute in Python 3

    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

    Comment

    • MRAB

      #3
      Re: Module __file__ attribute in Python 3

      On Aug 29, 6:11 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
      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.
      >
      +1. Leave __file__ as it is. Changing it seems an unnecessary break
      with previous versions.

      Comment

      Working...