Import Error When Adding a Database to a Flask App
<p>I'm following this <a href="http://blog.toast38coza.me/adding-a-database-to-a-flask-app/" rel="nofollow">tutorial</a> and when running <code>$ nosetests</code> in Step 2, I'm getting the following import errors:</p>

<pre><code>EE
======================================================================
ERROR: Failure: ImportError (No module named flask_sqlalchemy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/path/to/project/app/__init__.py", line 4, in &lt;module&gt;
    from flask_sqlalchemy import SQLAlchemy
ImportError: No module named flask_sqlalchemy

======================================================================
ERROR: Failure: ImportError (No module named flask_testing)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/path/to/project/tests.py", line 3, in &lt;module&gt;
    from flask_testing import TestCase
ImportError: No module named flask_testing

----------------------------------------------------------------------
Ran 2 tests in 0.150s

FAILED (errors=2)
</code></pre>

<p>With the help of <a href="http://stackoverflow.com/questions/18157494/python3-virtual-environment-and-pip">these</a> <a href="http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world" rel="nofollow">two</a> links, I'm running the following commands:</p>

<pre><code>// Set up virtual environment
$ cd project
$ python3 -m venv flask
$ source flask/bin/activate
$ (flask)$

// Install 
$ (flask)$ curl https://bootstrap.pypa.io/get-pip.py | python3

// Deactivate then reactivate virtual environment
(flask)$ deactivate
$ source flask/bin/activate

// Current status
(flask)$ pip -V
# pip 8.1.2 from /project/flask/lib/python3.5/site-packages (python 3.5)
(flask)$ python -V
# Python 3.5.2

// install modules
$ flask/bin/pip3 install flask
$ flask/bin/pip3 install flask-sqlalchemy
$ flask/bin/pip3 install flask-testing

// Updated status
(flask)$ pip list
# click (6.6)
# Flask (0.11.1)
# Flask-SQLAlchemy (2.1)
# Flask-Testing (0.6.1)
# itsdangerous (0.24)
# Jinja2 (2.8)
# MarkupSafe (0.23)
# pip (8.1.2)
# setuptools (20.10.1)
# SQLAlchemy (1.1.1)
# Werkzeug (0.11.11)
# wheel (0.29.0)
</code></pre>

<p>And have set up the following files:</p>

<pre><code>// tests.py
from flask_testing import TestCase
from app import db, app

TEST_SQLALCHEMY_DATABASE_URI = "sqlite:///test.sqlite"

class MyTest(TestCase):

def create_app(self):
    app.config['SQLALCHEMY_DATABASE_URI'] = TEST_SQLALCHEMY_DATABASE_URI
    return app

def setUp(self):
    db.create_all()

def tearDown(self):
    db.session.remove()
    db.drop_all()

// __init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.db'

// run.py
#!flask/bin/python3
from app import app
app.run(debug=True)
</code></pre>

<p>I'm trying to use python3 for everything and it seems it's looking in 2.7 for the modules. Can you tell me what I'm doing wrong? Thanks.</p>

<p>In Flask, usually the import errors of modules (though installed), directed to the way you are running the application. When you run the run.py, please check the interpreter path. I have seen some times, running ./run.py would not work since the libraries are under flask/* directories. 
You can either run ~/flask/bin/python run.py or add flask/bin/python to LD_LIBRARY_PATH</p>

python flask flask-sqlalchemy