title: Python Circular import: solutions and best practice
body: <p>I'm writing an application for <strong>scientific data analysis</strong> and I'm wondering what's the best way to structure the code to avoid (or address) the circular import problem. Currently  I'm using a mix of OO and procedural programming.</p>
  
  <p><a href="https://stackoverflow.com/questions/894864/circular-dependency-in-pythonhttps://stackoverflow.com/questions/894864/circular-dependency-in-python">Other questions</a> address this issue but in a more abstract way. Here I'm looking for a solution that is optimal in a more specific context.</p>
  
  <p>I have a class <code>Container</code> defined in <code>DataLib.py</code> whose data consist in lists and/or arrays. With all methods and supporting functions <code>DataLib.py</code> is quite large (~1000 lines). </p>
  
  <p>I have a second module <code>SelectionLib.py</code> (~400 lines) that contains only functions to "filter" the data in <code>Container</code> according to different criteria. These functions return new <code>Container</code> objects (with filtered data) and thus <code>SelectionLib.py</code> needs to import <code>Container</code> from <code>DataLib.py</code>. Note that, logically, these functions are "methods" for "Container", they are just implemented using python functions. </p>
  
  <p>Now, I want to add some high level method to <code>Container</code> so that a complex analysis can be performed with a single function of method call. And by  "complex analysis" I mean an arbitrary number of <code>Container</code> methods call, local function (defined in DataLib.py) and filter functions (defined in<code>SelectionLib.py</code>).</p>
  
  <p>So the problem is that <code>DataLib.py</code>  needs to import <code>SelectionLib.py</code> to use the filter functions, but <code>SelectionLib.py</code> already imports <code>DataLib.py</code>.</p>
  
  <p>Right know my hackish solution is to run the two files with <strong>run -i ...</strong> from <strong>IPython</strong> so it is like having a big single file and I avoid the circular import. But at the same time this scripts are difficult to integrate for example in a GUI.</p>
  
  <p>How do you suggest to solve this problem: </p>
  
  <ol>
  <li><p>use pure OO and inheritance and split the object in 3: <code>CoreContainer</code> -> <code>SelectionContainer</code> -> <code>HighLevelContainer</code></p></li>
  <li><p>Restructuring the code (everything in one file?)</p></li>
  <li><p>Some sort of <a href="http://effbot.org/zone/import-confusion.htm" rel="nofollow noreferrer">Import trickery</a> (put imports at the end)</p></li>
  </ol>
  
  <p>Any feedback is appreciated!</p>