繁体   English   中英

WSGI、Flask、Python 2.7、Apache2、ImportError = 没有名为 Flask 的模块

[英]WSGI, Flask, Python 2.7, Apache2, ImportError = no modul named flask

我正在按照本教程使用 WSGI Apache2 在 Ubuntu 上部署 Flask 这是我的目录的样子:

FlaskApp
    FlaskApp
        static
        templates
        __init__.py
    flaskapp.wsgi

我创建了一个 virtualenv。 我在 virtualenv 中安装了 Flask。 当我激活 virtualenv 时, __ini__.py启动网络服务器。

当我尝试连接到实时服务器时,出现内部服务器错误。

问题是 WSGI 没有找到 Flask。 但是安装了 Flask。 当我尝试在 virtualenv 中安装 Flask 时,我得到“要求已经满足:/usr/lib/python2.7/dist-packages (0.10.1) 中的烧瓶”。 那是问题吗?

我把编译后的版本改成了python 2.7

[Tue Oct 30 15:16:23.863324 2018] [mpm_event:notice] [pid 29283:tid 139720393185152] AH00491: caught SIGTERM, shutting down
[Tue Oct 30 15:16:23.943705 2018] [wsgi:warn] [pid 29915:tid 140238218004352] mod_wsgi: Compiled for Python/2.7.11.
[Tue Oct 30 15:16:23.943747 2018] [wsgi:warn] [pid 29915:tid 140238218004352] mod_wsgi: Runtime using Python/2.7.12.
[Tue Oct 30 15:16:23.944430 2018] [mpm_event:notice] [pid 29915:tid 140238218004352] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Tue Oct 30 15:16:23.944448 2018] [core:notice] [pid 29915:tid 140238218004352] AH00094: Command line: '/usr/sbin/apache2'
[Tue Oct 30 15:17:25.679253 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003] mod_wsgi (pid=29918): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Tue Oct 30 15:17:25.679276 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003] mod_wsgi (pid=29918): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Tue Oct 30 15:17:25.679292 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003] Traceback (most recent call last):
[Tue Oct 30 15:17:25.679304 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003]   File "/var/www/FlaskApp/flaskapp.wsgi", line 19, in <module>
[Tue Oct 30 15:17:25.679340 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003]     from FlaskApp import app as application
[Tue Oct 30 15:17:25.679349 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 1, in <module>
[Tue Oct 30 15:17:25.679371 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003]     from flask import Flask
[Tue Oct 30 15:17:25.679388 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003] ImportError: No module named flask
[Tue Oct 30 15:17:38.089586 2018] [wsgi:error] [pid 29918:tid 140237992269568]     [remote 95.91.245.250:11699] mod_wsgi (pid=29918): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Tue Oct 30 15:17:38.089614 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699] mod_wsgi (pid=29918): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Tue Oct 30 15:17:38.089629 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699] Traceback (most recent call last):
[Tue Oct 30 15:17:38.089644 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699]   File "/var/www/FlaskApp/flaskapp.wsgi", line 19, in <module>
[Tue Oct 30 15:17:38.089663 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699]     from FlaskApp import app as application
[Tue Oct 30 15:17:38.089670 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 1, in <module>
[Tue Oct 30 15:17:38.089679 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699]     from flask import Flask
[Tue Oct 30 15:17:38.089693 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699] ImportError: No module named flask
[Tue Oct 30 15:17:38.417619 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699] mod_wsgi (pid=29918): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Tue Oct 30 15:17:38.417638 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699] mod_wsgi (pid=29918): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Tue Oct 30 15:17:38.417660 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699] Traceback (most recent call last):
[Tue Oct 30 15:17:38.417674 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699]   File "/var/www/FlaskApp/flaskapp.wsgi", line 19, in <module>
[Tue Oct 30 15:17:38.417696 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699]     from FlaskApp import app as application
[Tue Oct 30 15:17:38.417702 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 1, in <module>
[Tue Oct 30 15:17:38.417711 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699]     from flask import Flask
[Tue Oct 30 15:17:38.417723 2018] [wsgi:error] [pid 29918:tid 140238109865728]     [remote 95.91.245.250:11699] ImportError: No module named flask

__init__.py

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, I love Digital Ocean!"
if __name__ == "__main__":
    app.run()

FlaskApp/flaskapp.wsgi

activator = '/var/www/FlaskApp/FlaskApp/venv/bin/activate_this.py'  # Looted from virtualenv; should not require modification, since it's defined relatively
with open(activator) as f:
    exec(f.read(), {'__file__': activator})

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'Add your secret key'

/etc/apache2/sites-enabled/FlaskApp.conf

<VirtualHost *:80>
                ServerName 18.196.125.229
                ServerAdmin admin@18.196.125.229
#               WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/lib/python2.7/
# WSGIDaemonProcess FlaskApp python-home=flask in /usr/local/lib/python2.7/dist-packages/
WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp
                 WSGIProcessGroup FlaskApp
                WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
                <Directory /var/www/FlaskApp/FlaskApp/>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/FlaskApp/FlaskApp/static
                <Directory /var/www/FlaskApp/FlaskApp/static/>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

我试图卸载烧瓶 - 在虚拟环境中重新安装烧瓶:

sudo pip 卸载烧瓶

/usr/local/lib/python2.7/dist-packages/pip-18.1- 
py2.7.egg/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old         version of cryptography ([1, 2, 3]) may cause slowdown.
  warnings.warn(warning, RequestsDependencyWarning)
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want     sudo's -H flag.
Uninstalling Flask-0.10.1:
  Would remove:
    /usr/lib/python2.7/dist-packages/Flask-0.10.1.egg-info
    /usr/lib/python2.7/dist-packages/flask
Proceed (y/n)? y
  Successfully uninstalled Flask-0.10.1

源 venv/bin/激活

须藤 pip 安装烧瓶

(venv) ubuntu@ip-172-26-9-95:/var/www/FlaskApp/FlaskApp$ sudo pip install Flask
/usr/local/lib/python2.7/dist-packages/pip-18.1-py2.7.egg/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
warnings.warn(warning, RequestsDependencyWarning)
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not     owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting Flask
  Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
100% |████████████████████████████████| 92kB 17.8MB/s
Requirement already satisfied: Jinja2>=2.10 in /usr/local/lib/python2.7/dist- 
packages (from Flask) (2.10)
Requirement already satisfied: itsdangerous>=0.24 in     /usr/local/lib/python2.7/dist-packages (from Flask) (1.1.0)
Requirement already satisfied: Werkzeug>=0.14 in /usr/local/lib/python2.7/dist-packages (from Flask) (0.14.1)
Requirement already satisfied: click>=5.1 in /usr/local/lib/python2.7/dist-packages (from Flask) (7.0)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/dist-packages (from Jinja2>=2.10->Flask) (1.0)
Installing collected packages: Flask
Successfully installed Flask-1.0.2

仍然相同的导入错误:没有名为烧瓶的模块

您的问题是您正在使用 sudo 执行 pip。

在 virtualenv 中,您可以访问和控制 pip 需要修改的所有文件。 通过使用 sudo 运行,您以超级用户身份执行,从而在 virtualenv 的上下文之外执行 pip。

通常,您应该谨慎使用 pip 和 sudo,因为从不太可靠的来源安装软件包时,您可能会发现自己安装了恶意软件。

要安装成功,

# Must be in virtualenv.
pip install flask

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM