apache 为什么'flask_bootstrap'导入失败?

8fq7wneg  于 2022-12-04  发布在  Apache
关注(0)|答案(2)|浏览(141)

运行flask应用时,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))

没什么区别。

gpfsuwkq

gpfsuwkq1#

因此,在Graham说服我我的“修复”(将两行移到<Directory>指令中)并不是真正的修复,可能是其他地方出错的迹象之后,我决定深入挖掘。
按照文档,特别是确认虚拟环境的位置,我惊讶地发现,当激活我的本地机上的虚拟环境(而不是通过wsgi应用程序):

sys.prefix = '/usr'

当我期望它是:

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

我不知道这是怎么发生的。也许是作为root做所有初始工作的结果。
不过,高兴地说,删除和重新制作虚拟环境,这次作为一个普通用户,一切似乎都很好。

4bbkushb

4bbkushb2#

**注意:**此答案在发布后被@Bridgey发现是错误的。请参见他的其他答案。

原来,问题出在那两行字上:

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>

公平地说,这是根据文档中的示例。
非常感谢所有评论的人:)

相关问题