title: Pythonic way to resolve circular import statements?
body: <p>I just inherited some code which makes me uneasy:  There is a testing library, full of classes corresponding to webpages on our site, and each webpage class has methods to automate the functionality on that page.</p>
  
  <p>There are methods to click the link between pages, which returns the class of the linked page.  Here's a simplified example:</p>
  
  <p>File homePageLib.py:</p>
  
  <pre><code>class HomePage(object):
      def clickCalendarLink(self):
          # Click page2 link which navigates browswer to page2
          print "Click Calendar link"
          # Then returns the page2 object
          from calendarLib import CalendarPage
          return CalendarPage()
  </code></pre>
  
  <p>File calendarLib.py:</p>
  
  <pre><code>class CalendarPage(object):
      def clickHomePageLink(self):
          # Click page1 link which navigates browswer to page1
          print "Click Home Page link"
          # Then return the page2 object
          from homePageLib import HomePage
          return HomePage()
  </code></pre>
  
  <p>This then allows the script file to click on pages and get the object as a return value from that method, meaning the script author won't have to keep instantiating new pages as they navigate around the site.  (I feel like this is an odd design, but I can't exactly put my finger on why, other than that it seems strange to have a method named 'clickSomeLink' and return an object of the resulting page.)</p>
  
  <p>The following script illustrates how a script would navigate around the site:  (I inserted <code>print page</code> to show how the page object changes)</p>
  
  <p>Script File:</p>
  
  <pre><code>from homePageLib import HomePage
  
  page = HomePage()    
  print page
  page = page.clickCalendarLink()
  print page
  page = page.clickHomePageLink()
  print page
  </code></pre>
  
  <p>which produces the following output:</p>
  
  <pre><code>&lt;homePageLib.HomePage object at 0x00B57570&gt;
  Click Calendar link
  &lt;calendarLib.CalendarPage object at 0x00B576F0&gt;
  Click Home Page link
  &lt;homePageLib.HomePage object at 0x00B57570&gt;
  </code></pre>
  
  <p>So, the part of this that I specifically feel most uneasy about are the <code>from ____ import ____</code> lines that end up all over.  These strike me as bad for the following reasons:</p>
  
  <ol>
  <li>I've always made it a convention to put all import statements at the top of a file.</li>
  <li>Since there may be multiple links to a page, this results in the same <code>from foo import bar</code> line of code in several places in a file.  </li>
  </ol>
  
  <p>The problem is, if we put these import statements at the top of the page, we get import errors, because (as per this example), HomePage imports CalendarPage and vice versa:</p>
  
  <p>File homePageLib.py</p>
  
  <pre><code>from calendarLib import CalendarPage
  
  class HomePage(object):
      def clickCalendarLink(self):
          # Click page2 link which navigates browswer to page2
          print "Click Calendar link"
          # Then returns the page2 object
  
          return CalendarPage()
  </code></pre>
  
  <p>File calendarLib.py</p>
  
  <pre><code>from homePageLib import HomePage
  
  class CalendarPage(object):
      def clickHomePageLink(self):
          # Click page1 link which navigates browswer to page1
          print "Click Home Page link"
          # Then return the page2 object
          return HomePage()
  </code></pre>
  
  <p>This results in the following error:</p>
  
  <pre><code>&gt;&gt;&gt; from homePageLib import HomePage
  Traceback (most recent call last):
    File "c:\temp\script.py", line 1, in ?
      #Script
    File "c:\temp\homePageLib.py", line 2, in ?
      from calendarLib import CalendarPage
    File "c:\temp\calendarLib.py", line 2, in ?
      from homePageLib import HomePage
  ImportError: cannot import name HomePage
  </code></pre>
  
  <p>(tips on how to better format python output?)</p>
  
  <p>Rather than perpetuating this style, I'd like to find a better way.  Is there a Pythonic way to deal with circular dependencies like this and still keep import statements at the top of the file?</p>