导入错误:没有名为sqlalchemy的模块

时间:2018-01-27 12:04:47

标签: python mod-wsgi apache2.4

我根据以下教程设置了Apache2 / WSGI / Flask应用程序:https://amunategui.github.io/idea-to-pitch/#installing-flask。我的工作就像一个魅力,但当我调整它并更改python文件以连接到SQL时,我可以在日志中找到/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

但是,我能够运行使用python从命令行导入sqlalchemy的脚本。而且我没有虚拟环境等(我只是按照上面链接的基本安装)。

因此,WSGI能够在第1行导入烧瓶,但在第2行不能导入sqlalchemy ...任何想法如何解决这个问题?谢谢!

查看文件:

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

<VirtualHost *:80>
    ServerName ...my_public_domain...
    ServerAdmin admin@mywebsite.com
    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:password@localhost/dbname")  
Base.metadata.bind = engine  
DBSession = sessionmaker(bind=engine)
session = DBSession()

app = Flask(__name__)

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

1 个答案:

答案 0 :(得分:0)

感谢指教@Graham Dumpleton

我最终设置了WSGIDaemonProcess(参见:http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/#configuring-apache)和python虚拟环境,以确保我知道要配置哪个python(参见:https://askubuntu.com/questions/244641/how-to-set-up-and-use-a-virtual-python-environment-in-ubuntu)。