title: Python: How to get rid of circular dependency involving decorator?
body: <p>I had got a following case of circular import (here severly simplified):</p>
  
  <p><code>array2image.py</code> conversion module:</p>
  
  <pre><code>import tuti
  
  @tuti.log_exec_time # can't do that, evaluated at definition time
  def convert(arr):
      '''Convert array to image.'''
      return image.fromarray(arr)
  </code></pre>
  
  <p><code>tuti.py</code> test utils module:</p>
  
  <pre><code>import array2image
  
  def log_exec_time(f):
      '''A small decorator not using array2image'''
  
  def debug_image(arr):
      image = array2image.convert(arr)
      image = write('somewhere')
  </code></pre>
  
  <p>It failed with NameError. This didn't look right to me, as there was really no circular dependency there. I was looking for a <em>neat</em> way to avoid that or an explanation... and half way through writing this question I found it.</p>
  
  <p>Moving the <code>import</code> below the decorator in <code>tuti.py</code> resolves NameError:</p>
  
  <pre><code>def log_exec_time(f):
      '''A small decorator not using array2image'''
  
  import array2image
  
  def debug_image(arr):
      image = array2image.convert(arr)
      image = write('somewhere')
  </code></pre>