【问题标题】:WSGI, Flask, Python 2.7, Apache2, ImportError = no modul named flaskUWSGI、Flask、Python 2.7、Apache2、ImportError = no module named flask
【发布时间】:2019-04-03 11:28:20
【问题描述】:

我正在关注本教程到Deploy Flask on Ubuntu with WSGI Apache2。这是我的目录的样子:

FlaskApp
    FlaskApp
        static
        templates
        __init__.py
    flaskapp.wsgi

我创建了一个虚拟环境。我在 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>

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

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/激活

sudo pip install Flask

(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

还是一样的 ImportError: No module named flask

【问题讨论】:

  • Apache 正在为 Python 3.5.2 进行编译,而您使用的是 2.7。也许这导致了问题?
  • 可以卸载flask,激活virtualenv,在虚拟环境中安装flask吗?
  • @Ruben Helsloot 我把编译后的版本改成2.7还是一样的错误
  • @VillageMonkey 我尝试卸载 - 按照描述重新安装烧瓶,但仍然相同 ImportError
  • 一旦你激活了你的virtualenv,你不需要使用sudo pip,只要你的virtualenv被激活就使用pip install Flask。

标签: python flask mod-wsgi


【解决方案1】:

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

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

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

要安装成功,

# Must be in virtualenv.
pip install flask

【讨论】:

    猜你喜欢
    • 2013-06-27
    • 2017-02-20
    • 2018-07-20
    • 2021-07-20
    • 1970-01-01
    • 2016-06-13
    • 2017-04-15
    • 2012-12-21
    相关资源
    最近更新 更多