title: How to use PyMongo with Flask Blueprints?
body: <p>What is the correct way of picking up <em>mongo</em> object inside Blueprints?</p>
  <p>Here is how I have my parent <code>login.py</code>:</p>
  <pre><code>app.config.from_object('config')
  from flask.ext.pymongo import PyMongo
  from child import child
  from child2 import child2
  
  
  app = Flask(__name__)
  app.register_blueprint(child2.child2)
  app.register_blueprint(child.child)
  </code></pre>
  <p>in my <code>child.py</code></p>
  <pre><code>from app import app
  from flask.ext.pymongo import PyMongo
  
  mongo = PyMongo(app)
  child = Blueprint('child', __name__)
  </code></pre>
  <p><code>child2.py</code> is the same structure as child:</p>
  <pre><code>from app import app
  from flask.ext.pymongo import PyMongo
      
  mongo = PyMongo(app)
  child2 = Blueprint('child2', __name__)
  </code></pre>
  <p>This is the error message I get:</p>
  <pre><code>raise Exception('duplicate config_prefix &quot;%s&quot;' % config_prefix)
  Exception: duplicate config_prefix &quot;MONGO&quot;
  </code></pre>
  <hr />
  <p>I've tried the following in the blueprint</p>
  <pre><code>mongo = app.data.driver
  </code></pre>
  <p>but that raises error. Here is full traceback:</p>
  <pre><code>    Traceback (most recent call last):
    File &quot;login.py&quot;, line 12, in &lt;module&gt;
      from child import child
    File &quot;/home/xxx/xxx/child/child.py&quot;, line 13, in &lt;module&gt;
      mongo = PyMongo(app) #blueprint
    File &quot;/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py&quot;, line 97, in __init__
      self.init_app(app, config_prefix)
    File &quot;/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py&quot;, line 121, in init_app
      raise Exception('duplicate config_prefix &quot;%s&quot;' % config_prefix)
  Exception: duplicate config_prefix &quot;MONGO&quot;
  (xxx)xxx@linux:~/xxx$ python login.py 
  Traceback (most recent call last):
    File &quot;login.py&quot;, line 12, in &lt;module&gt;
      from courses import courses
    File &quot;/home/xxx/xxx/child/child.py&quot;, line 13, in &lt;module&gt;
      mongo = PyMongo(app) #blueprint
    File &quot;/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py&quot;, line 97, in __init__
      self.init_app(app, config_prefix)
    File &quot;/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py&quot;, line 121, in init_app
      raise Exception('duplicate config_prefix &quot;%s&quot;' % config_prefix)
  Exception: duplicate config_prefix &quot;MONGO&quot;
  </code></pre>
  <p>Once my app has created the connection, how should I pick it up in my blueprints?</p>
  <p>So the question is how can one structure the connection strings to the db in each of the blueprints. Here is the file structure:</p>
  <pre><code>login.py
  config.py
  /child/child.py
  /child2/child2.py
  </code></pre>
  <p>here is the <code>config.py</code></p>
  <pre><code>MONGO_DBNAME = 'xxx'
  
  MONGO_URL = os.environ.get('MONGO_URL')
  if not MONGO_URL:
      MONGO_URL = &quot;mongodb://xxx:xxxx@xxxx.mongolab.com:55822/heroku_xxx&quot;;
  
  MONGO_URI = MONGO_URL
  </code></pre>
  <p>I've tried the suggestion below in answers, but this did not work. See my comments below that prospective answer.</p>