[Fixed]-Cross import in the same directory

0👍

Putting imports at the end of your a.py and b.py should fix issues.
If it doesn’t help you try to move import into class definition block, so replace

from .a import TestA
Class TestB(object):
    pass

to

Class TestB(object):
    from .a import TestA
    pass

Small Suggestion: do not use relative imports.

1👍

There’re several approaches you can use to address the cross-imports problem:

  • Re-organize your modules so that there’re no cross-imports, i.e. put the classes to a single module, etc.

  • Replace from module import foo with import module and use full names.

  • Put imports at the end of modules (not recommended).

See also Circular imports in Python.

Leave a comment