Ad

Import Error: No Module Named Sqlalchemy

- 1 answer

I set up a Apache2/WSGI/Flask app based on the following tutorial: https://amunategui.github.io/idea-to-pitch/#installing-flask. 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/project.py", 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:

/etc/apache2/sites-available/FirstApp.conf

<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>
    <Directory /var/www/FlaskApps/FirstApp/static/>
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

/var/www/FlaskApps/FlaskApps.wsgi

#! /usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApps/FirstApp/")

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

/var/www/FlaskApps/FirstApp/project.py

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

@app.route('/')
def project():
    return  "This is from Flask!!!"
...
Ad

Answer

Thanks for the pointers @Graham Dumpleton

I ended up setting WSGIDaemonProcess (see: http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/#configuring-apache) and python virtual environments to ensure I knew which python to configure (see: https://askubuntu.com/questions/244641/how-to-set-up-and-use-a-virtual-python-environment-in-ubuntu).

Ad
source: stackoverflow.com
Ad