为什么 `flask_bootstrap` 导入失败?

Why does `flask_bootstrap` fail to import?

当运行一个flask app时,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

我已经按照 myapp.conf

配置了 venv
<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))

没有任何区别。

原来问题出在这两行:

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 中的示例。

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

所以,在 Graham 说服我说我的 "fix"(将这两行移到 <Directory> 指令中)并不是真正的解决办法并且可能是其他地方出了问题的迹象之后,我决定深入挖掘更深。

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

sys.prefix = '/usr'

当我期望它是:

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

我不知道这是怎么发生的。也许是作为 root.

完成所有初始工作的结果

不过,庆幸的是,删掉重做虚拟环境,这次作为普通用户,一切似乎都还好。