[issue21553] Behaviour of modules depends on how they where imported

Peter Otten report at bugs.python.org
Thu May 22 19:17:27 CEST 2014


Peter Otten added the comment:

Here's a simpler demo for what I believe you are experiencing:

$ mkdir package
$ cd package/
$ touch __ini__.py module.py
$ export PYTHONPATH=..
$ python3
Python 3.3.2+ (default, Feb 28 2014, 00:52:16) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import module, package.module
>>> module is package.module
False

Even though module and package.module correspond to the same file
Python does not recognize that you intend the former to be part of a package. Your run0.py script goes through its sys.path entries and unfortunately the first entry points into a package so that all modules in that package become also visible as toplevel modules.

A good way to avoid this trap is to use relative imports

>>> from . import module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: Parent module '' not loaded, cannot perform relative import

----------
nosy: +peter.otten

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21553>
_______________________________________


More information about the Python-bugs-list mailing list