seems like a bug in isinstance()

Gregory Ewing greg.ewing at canterbury.ac.nz
Sat May 7 04:53:40 EDT 2011


Chris Rebert wrote:
> This is because you did `from Point import
> ...` in file2.py, whereas in file1.py you did `from
> openopt.kernel.Point import ...`. These 2 different ways of referring
> to the same module are sufficient to "trick"/"outsmart" (C)Python and
> cause it to import the same module twice

That can't be the whole story, because those two ways of
referring to the module *should* have returned the same
module object, even though one is absolute and the other
relative.

I suspect what's happened is that somehow sys.path contains
both the directory containing .../openopt *and* the directory
.../openopt/kernel, and the second import is picking up
Point.py a second time as though it were a top-level module.

This could have happened by starting an interactive session
while cd'ed to .../openopt/kernel, or by launching a .py
file from there as a main script.

The solution is to make sure that sys.path only contains the
top level directories of the package hierarchy, and doesn't
also contain any of their subdirectories.

Always using absolute imports may be a good idea stylistically,
but in this case it will only mask the symptoms of whatever
is really wrong, and further problems could result from the
same cause.

-- 
Greg



More information about the Python-list mailing list