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

时间:2022-06-05 18:10:10

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:

我根据以下教程设置了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

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

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

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

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

See the files:

看文件:

/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 个解决方案

#1


0  

Thanks for the pointers @Graham Dumpleton

感谢指点@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).

我最终设置了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)。

#1


0  

Thanks for the pointers @Graham Dumpleton

感谢指点@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).

我最终设置了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)。