1

I'm following this tutorial and when running $ nosetests in Step 2, I'm getting the following import errors:

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 <module>
    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 <module>
    from flask_testing import TestCase
ImportError: No module named flask_testing

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

FAILED (errors=2)

With the help of these two links, I'm running the following commands:

// 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)

And have set up the following files:

// 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)

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.

Community
  • 1
  • 1
kd.
  • 64
  • 5

1 Answers1

0

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

zXi
  • 112
  • 7