problem with isinstance and relative/absolute import

Martin v. Loewis martin at v.loewis.de
Sun Jun 9 15:57:23 EDT 2002


Sylvain Thenault <thenault at nerim.net> writes:

> actually I did understood what was the problem, I was only asking if this
> behaviour should be considered as normal...

Yes, certainly.

> Since it's current in a package to mix relative (to the python file
> directory) and absolute (package) import, isn't there a risk to
> obtain a strange behaviour without being in the package directory ?

If the current directory is not inside a package, then relative
imports won't resolve through sys.path, but instead will resolve
through the package which executes the import. Hence, the imported
module will correctly show up as a submodule.

Notice that you can produce the same problem in Java, which solves
this problem with the package declaration on top of each file (so the
problem exists only if you omit the package declaration).

Guido is tempted to solve the problem by banning relative imports.

Regards,
Martin



More information about the Python-list mailing list