Import Error: No Module Named Sqlalchemy

- 1 answer

I set up a Apache2/WSGI/Flask app based on the following tutorial: I worked like a charm but when I tweaked it and changed the python file in order to connect to SQL, I can find in the logs at /var/log/apache2/error.log:

File "/var/www/FlaskApps/FlaskApps.wsgi", line 8, in <module>
from project import app as application
File "/var/www/FlaskApps/FirstApp/", line 2, in <module>
from sqlalchemy import create_engine
ImportError: No module named sqlalchemy

However, I am able to run scripts that import sqlalchemy from the command line with python. And I don't have virtual envs etc (I just followed the basic installation from the link above).

So WSGI is able to import flask on line 1, but not sqlalchemy on line 2... Any idea how to fix this? Thanks!

See the files:


<VirtualHost *:80>
    ServerName ...my_public_domain...
    ServerAdmin [email protected]
    WSGIScriptAlias / /var/www/FlaskApps/FlaskApps.wsgi
    <Directory /var/www/FlaskApps/FirstApp/>
        Order allow,deny
        Allow from all
    <Directory /var/www/FlaskApps/FirstApp/static/>
        Order allow,deny
        Allow from all
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined


#! /usr/bin/python
import sys
import logging

# home points to the file
from project import app as application
application.secret_key = "somesecretsessionkey"


from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Restaurant, Base, MenuItem

engine = create_engine("mysql+mysqldb://root:[email protected]/dbname")  
Base.metadata.bind = engine  
DBSession = sessionmaker(bind=engine)
session = DBSession()

app = Flask(__name__)

def project():
    return  "This is from Flask!!!"


Thanks for the pointers @Graham Dumpleton

I ended up setting WSGIDaemonProcess (see: and python virtual environments to ensure I knew which python to configure (see: