title: Flask - How can I prevent circular imports in a big app?
body: <p>I am working on a project in Flask and am having trouble with circular imports.</p>
  
  <p>My app's structure looks like this:</p>
  
  <pre><code>.
  ├── api
  │   ├── __init__.py
  │   ├── schema.py
  │   └── sender.py
  ├── app.py
  ├── config.py
  ├── README.md
  ├── run.sh
  ├── static
  │   ├── css
  │   │   ├── base.css
  │   │   ├── index.css
  │   │   └── model.css
  │   └── index.html
  ├── templates
  │   ├── base.html
  │   ├── model.html
  │   ├── schema_add.html
  │   ├── schema.html
  │   └── table.html
  └── views
      ├── auth.py
      ├── error_handler.py
      ├── __init__.py
      ├── model.py
      ├── schema.py
      └── table.py
  </code></pre>
  
  <p>The source code is available on <a href="https://github.com/shubham1172/anton/tree/fc2748cb8e2307167facb1e26357bd7f81e3429b" rel="nofollow noreferrer">this</a></p>
  
  <pre><code>Here's a trace of error:
      Traceback (most recent call last):
    File "./../anton_temp/app.py", line 3, in &lt;module&gt;
      from views import *
    File 
  "/home/shubham1172/Documents/Anton/anton_temp/views/__init__.py", line 
  16, in &lt;module&gt;
      from .auth import *
    File "/home/shubham1172/Documents/Anton/anton_temp/views/auth.py", 
  line 4, in &lt;module&gt;
      from app import setConnection, getConnection, closeConnection
    File "/home/shubham1172/Documents/Anton/anton_temp/app.py", line 4, 
  in &lt;module&gt;
      from api import *
    File "/home/shubham1172/Documents/Anton/anton_temp/api/__init__.py", 
  line 13, in &lt;module&gt;
      from .schema import *
    File "/home/shubham1172/Documents/Anton/anton_temp/api/schema.py", 
  line 5, in &lt;module&gt;
      from app import getConnection
  ImportError: cannot import name 'getConnection'
  </code></pre>
  
  <p>The problem is, I have to include my blueprints (views and api) in my app.
  The <strong>init</strong> file for these blueprints further include the py files which in turn has to include some functions from the app.</p>
  
  <p>I read somewhere to include these functions in an external file, say extension.py and then call it from the blueprints, but my functions in app contains references to 'app' for its config object. </p>
  
  <p>How do I fix this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As pointed out, I'll have to refactor my code.
  <a href="https://stackoverflow.com/questions/33398673/beating-circular-references-in-flask-blueprints">This</a> shows an example of the same issue and the solution was given. However, the function in my extension would require a call to the app's config, i.e.</p>
  
  <p><em>A.py</em> </p>
  
  <pre><code>import B
  from C import dependency
  </code></pre>
  
  <p><em>B.py</em></p>
  
  <pre><code>from C import dependency
  </code></pre>
  
  <p><em>C.py</em></p>
  
  <pre><code>def dependency():
      #Use A.config here &lt;----------
      pass
  </code></pre>
  
  <p>Is there any way to solve this? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I solved it by refactoring my code.
  I figured out that app.config can be exported to a different file with a simple function call.</p>
  
  <p><em>C.py</em></p>
  
  <pre><code>obj = None
  def setObj(object):
      obj = object
  
  def dependency():
      #use obj now
      pass    
  </code></pre>
  
  <p>setObj(obj) can now be called from A.py!</p>