无法在 Raspberry pi 上的 Python3.5 下使用 apache2、mod_wsgi 导入 flask_cors

Impossible to import flask_cors with apache2, mod_wsgi under Python3.5 on a Raspberry pi

我尝试在 Raspberry Pi 3B+、Apache2 (2.4.25) 和 mod_wsgi 上建立一个基于 Flask 的网站,但我无法导入模块 flask_cors 在 Python3.5...

我用命令 ssudo apt-get install libapache2-mod-wsgi

安装了 mod_wsgi

因为我只打算使用 Raspberry 来托管我的网站,所以我尝试在系统范围内安装 Flask 所需的软件包,版本为 Python,使用以下命令:pip3 install -r requirements.txt 其中 requirements.txt 如下:

click==6.7
Flask==0.12.3
Flask-Cors==3.0.3
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
six==1.11.0
Werkzeug==0.12.2
Requests==2.20.0

我想我明白在 Raspi 上,最好使用命令 pip3 install -U <module> 导入模块,因为这些包没有安装在 python 的默认位置,这在我的案例通过以下命令 python -m site 产生以下信息:

sys.path = [
    '/home/pi/.local/lib/python3.5/site-packages',
    '/usr/lib/python35.zip',
    '/usr/lib/python3.5',
    '/usr/lib/python3.5/plat-arm-linux-gnueabihf',
    '/usr/lib/python3.5/lib-dynload',
    '/usr/local/lib/python3.5/dist-packages',
    '/usr/local/lib/python3.5/dist-packages/virtualenv-16.1.0-py3.5.egg',
    '/usr/lib/python3/dist-packages',
]
USER_BASE: '/home/pi/.local' (exists)
USER_SITE: '/home/pi/.local/lib/python3.5/site-packages' (exists)
ENABLE_USER_SITE: True

我的Flask和依赖确实安装在/home/pi/.local/lib/python3.5/site-packages文件夹中。但这似乎不是问题,因为在 python shell 上检查导入时,系统范围内的一切似乎都很好:

pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask_cors import CORS
>>> 

知道这一点后,我尝试将以下文件用于设置我的 apache 服务器:

flaskregul.conf 在文件夹 /etc/apache2/sites-enabled:

<VirtualHost *:80>
    WSGIScriptAlias / /var/www/flaskregul/flaskregul.wsgi
    Alias /static/ /var/www/flaskregul/dist/static/
    <Directory /var/www/flaskregul/dist/static>
        WSGIProcessGroup flaskregul
            WSGIApplicationGroup %{GLOBAL}
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog /var/log/apache2/flask.monnomdedomaine.com.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

flaskregul.wsgi 在 /var/www/flaskregul:

import sys
sys.path.insert(0,"/var/www/flaskregul") # path to my __init__.py file
sys.path.insert(0,"/home/pi/.local/lib/python3.5/site-packages") # path to the site-packages folder where the modules are installed by pip3
from __init__ import app as application

__init__.py 在 /var/www/flaskregul:

from flask import Flask
from flask_cors import CORS

我无法专门导入 flask_cors 模块,因为启动文件 __init__.py 会在文件 /var/log/apache2/flask.monnomdedomaine.com.log 中产生以下错误:

[Sat Nov 10 00:37:22.220394 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] mod_wsgi (pid=6804): Target WSGI script '/var/www/flaskregul/flaskregul.wsgi' cannot be loaded as Python module.
[Sat Nov 10 00:37:22.220972 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] mod_wsgi (pid=6804): Exception occurred processing WSGI script '/var/www/flaskregul/flaskregul.wsgi'.
[Sat Nov 10 00:37:22.221936 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] Traceback (most recent call last):
[Sat Nov 10 00:37:22.222365 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550]   File "/var/www/flaskregul/flaskregul.wsgi", line 1, in <module>
[Sat Nov 10 00:37:22.222395 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550]     from __init__ import app as application
[Sat Nov 10 00:37:22.222445 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550]   File "/var/www/flaskregul/__init__.py", line 2, in <module>
[Sat Nov 10 00:37:22.222463 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550]     from flask_cors import CORS
[Sat Nov 10 00:37:22.222557 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] ImportError: No module named 'flask_cors'

而 Flask 的导入似乎很好...

我试过通过这个更改 WSGI 脚本文件来调试它:

import sys
sys.path.insert(0,"/var/www/flaskregul")
sys.path.insert(0,"/home/pi/.local/lib/python3.5/site-packages")

def application(environ, start_response):
    status = '200 OK'

    output = u''
    output += u'sys.version = %s\n' % repr(sys.version)
    output += u'sys.prefix = %s\n' % repr(sys.prefix)
    output += u'sys.path = %s\n' % repr(sys.path)

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output.encode('UTF-8')] 

这是来自 mod_wsgi:https://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html 的网页上的一条建议。打开网页时,我得到这样的结果:

sys.version = '3.5.3 (default, Sep 27 2018, 17:25:39) \n[GCC 6.3.0 20170516]'
sys.prefix = '/usr'
sys.path = ['/home/pi/.local/lib/python3.5/site-packages', '/var/www/flaskregul', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-arm-linux-gnueabihf', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/local/lib/python3.5/dist-packages/virtualenv-16.1.0-py3.5.egg', '/usr/lib/python3/dist-packages']

我的印象是路径是正确的!我实际上成功地导入了位于 /var/www/flaskregul 位置的我自己的模块...

我迷路了,我不明白为什么模块 flask_cors 不可用,因为它确实在路径中:

pi@raspberrypi:~/.local/lib/python3.5/site-packages $ l | grep flask
drwxr-xr-x 5 pi pi   4096 nov.   9 23:30 flask
drwxr-xr-x 3 pi pi   4096 nov.  10 00:08 flask_cors
pi@raspberrypi:~/.local/lib/python3.5/site-packages $ l flask_cors 
total 44
-rw-r--r-- 1 pi pi 13771 nov.  10 00:08 core.py
-rw-r--r-- 1 pi pi  4937 nov.  10 00:08 decorator.py
-rw-r--r-- 1 pi pi  7405 nov.  10 00:08 extension.py
-rw-r--r-- 1 pi pi   924 nov.  10 00:08 __init__.py
drwxr-xr-x 2 pi pi  4096 nov.  10 00:08 __pycache__
-rw-r--r-- 1 pi pi    22 nov.  10 00:08 version.py

非常感谢任何帮助!我目前正在以令人担忧的速度脱发!

我的天哪我在放弃之前拼命尝试解决了它。

我在shell上执行了以下命令:(pip是python3.5/!\的pip)

pip uninstall flask
pip uninstall flask_cors
sudo pip install --upgrade pip
hash -d pip
sudo pip install -U flask
sudo pip install -U flask_cors

网站上线运行! :D