为什么`flask_bootstrap`无法导入?

时间:2018-05-03 17:27:38

标签: python python-2.7 apache virtualenv mod-wsgi

运行烧瓶应用时,Apache2的error.log显示无法找到flask_bootstrap模块:

[wsgi:warn] mod_wsgi: Compiled for Python/2.7.11.
[wsgi:warn] mod_wsgi: Runtime using Python/2.7.12.
[mpm_event:notice] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[core:notice] AH00094: Command line: '/usr/sbin/apache2'
[wsgi:error] mod_wsgi (pid=18587): Target WSGI script '/var/www/myapp/myapp.wsgi' cannot be loaded as Python module.
[wsgi:error] mod_wsgi (pid=18587): Exception occurred processing WSGI script '/var/www/myapp/myapp.wsgi'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error]   File "/var/www/myapp/myapp.wsgi", line 7, in <module>
[wsgi:error]     from myapp import app as application 
[wsgi:error]   File "/var/www/myapp/myapp/__init__.py", line 2, in <module>
[wsgi:error]     from flask_bootstrap import Bootstrap
[wsgi:error] ImportError: No module named flask_bootstrap

我已根据venv

配置了myapp.conf
<VirtualHost *:80>
ServerName yourdomain.com
ServerAdmin youemail@email.com

WSGIProcessGroup myapp
WSGIApplicationGroup %{GLOBAL}

WSGIScriptAlias / /var/www/myapp/myapp.wsgi
WSGIDaemonProcess myapp python-home=/var/www/myapp/myapp/venv

<Directory /var/www/myapp/myapp/>
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

该模块可在系统范围内和在venv中使用:

root@host:/var/www/myapp# python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask_bootstrap import Bootstrap
>>> Bootstrap
<class 'flask_bootstrap.Bootstrap'>

和...

root@host:/var/www/myapp# source myapp/venv/bin/activate
(venv) root@host:/var/www/myapp# python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask_bootstrap import Bootstrap
>>> Bootstrap
<class 'flask_bootstrap.Bootstrap'>

我注意到有关于2.7.11与2.7.12不匹配的警告,但是次要版本真的是问题吗?

修改1

根据the docs,将以下内容添加到myapp.wsgi

activate_this = '/var/www/myapp/myapp/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

没有任何区别。

2 个答案:

答案 0 :(得分:1)

所以,在格雷厄姆说服我之后,我的“修复”(将这两行移到<Directory>指令中)并不是真正的修复,可能是其他错误的标志,我决定深入挖掘。

根据文档,特别是confirming the location of the virtual environment,我惊讶地发现在我的本地盒子上激活虚拟环境时(不是通过wsgi应用程序):

sys.prefix = '/usr'

当我预料到的时候:

sys.prefix = '/var/www/myapp/myapp/venv'

我不知道这是怎么发生的。也许是以root完成所有初始工作的结果。

然而,很高兴地说,删除和重新构建虚拟环境,这次作为普通用户,一切似乎都很好。

答案 1 :(得分:0)

事实证明问题在于两行:

WSGIProcessGroup myapp
WSGIApplicationGroup %{GLOBAL}

应该在<Directory>

所以,myapp.conf现在看起来像这样:

<VirtualHost *:80>
        ServerName yourdomain.com

        WSGIDaemonProcess myapp python-home=/var/www/myapp/myapp/venv
        WSGIScriptAlias / /var/www/myapp/myapp.wsgi

        <Directory /var/www/myapp/myapp>
                WSGIProcessGroup myapp
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

公平地说,这是the docs中的例子。

非常感谢所有评论的人:)