As @wRAR mentioned, loading a module can mean executing any amount of code, which can take some time. On the other hand, the module will be loaded only once, and any subsequent import attempt will find the module present in os.sys.modules and refer to it.
In the Django environment in debug mode, the modules are removed from the Django AppCache and are actually re-imported only when they are changed, which you probably will not do with ipdb , so this should not be a problem in your case.
However, in cases where this would be a problem, there are some ways around this. Suppose you have a custom module that you use to load anyway, you can add a function to it that imports ipdb only when you need it:
in the module you want to use ipdb.set_trace :
import mymodule mymodule.set_trace()
or, at the top of your module, use the __debug__ cross-module __debug__ :
if __debug__: from ipdp import set_trace else: def set_trace(): return
Remi
source share