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

时间:2018-11-10 00:04:11

标签: apache flask pip raspberry-pi3 wsgi

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

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

安装了mod_wsgi

因为我只打算使用Raspberry来托管我的网站,所以我尝试使用以下命令安装Python的3.5版系统范围内Flask所需的软件包:pip3 install -r requirements.txt where 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服务器:

/ p / apache2 / sites-enabled文件夹中的

flaskregul.conf:

<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>
/ var / www / flaskregul中的

flaskregul.wsgi:

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
在/ var / www / flaskregul中

__ init __。py:

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

任何帮助将不胜感激!我目前正在以令人担忧的速度脱发!

1 个答案:

答案 0 :(得分:0)

我的天哪,我在放弃之前拼命地解决了这个问题。

我在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