【问题标题】:import flask on wsgi virtual host failswsgi 虚拟主机上的导入烧瓶失败
【发布时间】:2015-05-15 11:00:46
【问题描述】:

具有以下目录结构和设置:

.
├── app
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── static
│   ├── templates
│   │   ├── base.html
│   │   └── index.html
│   ├── views.py
│   └── views.pyc
├── flask
├── myapp.wsgi
├── run.py
├── run.pyc
└── tmp

myapp.wsgi

#!/usr/bin/python
import sys, os, logging
logging.basicConfig(stream=sys.stderr)
#sys.path.insert(0,"/var/www/otherStuff/myApp/")
sys.path.insert(0, os.path.dirname(__file__))

#from app import app
from app import app as application
application.secret_key = 'xyz'

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True)

__init__.py

from flask import Flask

app = Flask(__name__)
from app import views

vhost.conf

<VirtualHost *:80>
        ServerName local.myapp.com
        WSGIScriptAlias / /var/www/otherStuff/myApp/myappwsgi
        <Directory /var/www/otherStuff/myApp/app>
                Order allow,deny
                Allow from all
        </Directory>
        #Alias /static /var/www/otherStuff/myApp/static
        #<Directory /var/www/otherStuff/myApp/static>
        #       Order allow,deny
        #       Allow from all
        #</Directory>
        ErrorLog /var/www/logs/myApp-error.log
        LogLevel warn
        CustomLog /var/www/logs/myApp-access.log combined
</VirtualHost>

我似乎无法弄清楚,为什么没有加载 Flask 依赖项,因此它无法运行

mod_wsgi (pid=19668): Target WSGI script '/var/www/otherStuff/myApp/myapp.wsgi' cannot be loaded as Python module., referer: http://local.myapp.com/
mod_wsgi (pid=19668): Exception occurred processing WSGI script '/var/www/otherStuff/myApp/myapp.wsgi'., referer: http://local.myapp.com/
Traceback (most recent call last):, referer: http://local.myapp.com/
  File "/var/www/otherStuff/myApp/myapp.wsgi", line 8, in <module>, referer: http://local.myapp.com/
    from app import app as application, referer: http://local.myapp.com/
  File "/var/www/otherStuff/myApp/app/__init__.py", line 1, in <module>, referer: http://local.myapp.com/
    from flask import Flask, referer: http://local.myapp.com/
ImportError: No module named flask, referer: http://local.myapp.com/

更新

flask 是虚拟环境:

flask/
├── bin
├── include
├── lib
├── lib64 -> /home/alexb/www/otherStuff/faqColab/flask/lib
├── local
└── pyvenv.cfg

【问题讨论】:

  • 你安装flask了吗?如何,在哪里?
  • @DanielRoseman 嗯├── flask;它在那里
  • 我建议您学习将 Flask 作为 Python 包安装并为您的安装配置 virtualenv,而不是手动复制项目文件夹中的任何内容 - 这样您就可以使用 Flask 和使用标准 Python 的任何其他 Python 包工具。如何安装 Python 包的官方指南:packaging.python.org/en/latest/installing.html
  • mmm...这是虚拟环境,没有手动复制:\ virtualenv flask

标签: python apache flask wsgi


【解决方案1】:

您需要告诉 mod_wsgi 如何在您的虚拟环境中查找包。我不确定我的文件夹结构完全正确,但应该是这样的。

WSGIPythonPath /var/www/otherStuff/myApp:/var/www/otherStuff/myApp/flask/lib/python2.7/site-packages

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多