Uncategorized

Differences between import filename and import directoryname in Python


In Python, when we do import modulename, is there any material difference between

  • a file modulename.py
  • a directory with the same contents in modulename/__init__.py?

Concretely: consider a file m.py containing s = "hello":

>>> import m
>>> m.s
'hello'

If instead we have a directory m with a file __init__.py containing the same s = "hello", we get the identical result.

Obviously the value of __file__ is different during execution, and the directory structure can have submodules.

Are there any other differences at all? With any variant of import (from/as/etc) ?


I’m really only concerned with Python 3, but version-specific details also welcomed for historic interest.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *