One of the annoying problems that you are likely to face while working with modules is what is known as circular dependencies. To understand what these are, consider the following two modules:
# module_1.py from module_2 import calc_markup def calc_total(items): total = 0 for item in items: total = total + item['price'] total = total + calc_markup(total) return total # module_2.py from module_1 import calc_total def calc_markup(total): return total * 0.1 def make_sale(items): total_price = calc_total(items) ...
While this is a contrived example, you can see that module_1
imports something from module_2
, and module_2
imports something from module_1
. If you tried to run a program containing these two modules, you would see the following error when module_1
is imported:
ImportError: cannot import name calc_total
If you tried to import module_2
instead, you would get a similar error. With the code organized in this way, you're stuck...