Since the interest of this topic is to get full names, here is the trap that arises when using relative imports along with the main module existing in the same package. For example, with the module settings below:
$ cat /tmp/fqname/foo/__init__.py $ cat /tmp/fqname/foo/bar.py from baz import Baz print Baz.__module__ $ cat /tmp/fqname/foo/baz.py class Baz: pass $ cat /tmp/fqname/main.py import foo.bar from foo.baz import Baz print Baz.__module__ $ cat /tmp/fqname/foo/hum.py import bar import foo.bar
Here is the output showing the result of importing the same module in different ways:
$ export PYTHONPATH=/tmp/fqname $ python /tmp/fqname/main.py foo.baz foo.baz $ python /tmp/fqname/foo/bar.py baz $ python /tmp/fqname/foo/hum.py baz foo.baz
When hum imports bar using the relative path, bar sees Baz.__module__ as just "baz", but in the second import, which uses the full name, bar sees the same as "foo.baz".
If you keep full names somewhere, it is best to avoid relative imports for these classes.
haridsv Apr 01 '10 at 23:50 2010-04-01 23:50
source share