The following sentence, after you indicate in your question, confirms that the names defined in the package ("variables" to use your wording) take precedence over submodules / packages:
The import statement first checks to see if an item is defined in the package; if not, it assumes that it is a module and tries to load it.
I cannot find explicit confirmation in the document for Python 2.7 that packages take precedence over modules. I found this in PEP 420 :
During import processing, the import engine will continue to iterate over each directory in the parent path, as it does in Python 3.2. When searching for a module or package named "foo" for each directory in the parent path:
- If
<directory>/foo/__init__.py , the regular package will be imported and returned. - If not, but
<directory>/foo.{py,pyc,so,pyd} found, the module is imported and returned.
... which, although it only explicitly states that this behavior is in Python 3.2, one might assume that "... and previous versions of Python." Again, this confirms your view that packages take precedence over modules.
However: it would be a terrible idea to rely on this implementation detail. The number of people in the world who are aware of this probably does not go far beyond the core Python developers; in every sense and purpose it is undocumented and can cause extremely difficult to track errors.
source share