How to avoid mutual imports

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Fri Apr 12 14:41:09 EDT 2002


I need advice on how to handle the following situation:

utils1.py:
    a
	b
    c
    if __name__ == '__main__':
	   from interface import data_initialization  # circular
	   test_a()
	   test_b()
	   test_c()

utils2.py:
    x
	y
    z
    if __name__ == '__main__':
	   from interface import data_initialization  # circular
	   test_x()
	   test_y()
	   test_z()

interface.py:
	from utils1 import a, b, c
	from utils2 import x, y, z
	data_initialization

application.py:
	from interface import data_initialization
	do_something()


Note that the interface module defines the data initialization that depends
on both utils1 and utils2.  And the testing of utils1 and utils2 both depend
on data being initialized for both.

Of course I could put everything into a single module, but that would make
it very large.  The definitions of a,b,c and x,y,z really are independent.

Another idea is to write separate tests for each module.  Yes I have done
that, but many things that I want to test rely on data for both modules.

Huaiyu



More information about the Python-list mailing list