【发布时间】:2017-12-06 04:37:17
【问题描述】:
我正在尝试学习一本关于 Flask 的介绍性书籍/教程,该教程的一部分要求您在虚拟专用服务器上设置本地开发环境和生产环境。
简而言之,我收到一个看起来像 Python 导入错误的错误,抱怨它找不到 Flask。
我浏览了其他 Flask 导入错误堆栈溢出帖子,但无济于事:我不相信我的 python 版本与 mod_wsgi 不匹配,并且我已将我认为是 Flask 目录的内容插入到路径中(大约两种不同的方式,没有任何效果)。我想知道是否存在问题,因为 Google Compute Engine 可能以某个特定用户身份运行 apache,而 Flask 安装到“~”的子文件夹中,我相信这取决于我在 SSH 时使用的用户名 - 换句话说,也许Apache 看不到该文件夹或任何“用户文件夹”?我对 Linux 没有经验,所以正在努力缩小问题的范围。
但总的来说,当通过 mod_wsgi 执行 hello.py 时,我该如何纠正它的导入错误?
非常感谢勇敢的灵魂敢于继续阅读!下面有更多详细信息,包括文件/内容、重现步骤等。
当我访问应显示 hello.py 应用程序的页面时,我收到 500 内部服务器错误。查阅日志显示以下错误(我添加了一些 python 打印语句以了解发生了什么):
[Mon Jul 03 00:58:21.619295 2017] [wsgi:error] [pid 4520:tid
139990843242240] PYTHON SYS.PATH:
[Mon Jul 03 00:58:21.619440 2017] [wsgi:error] [pid 4520:tid 139990843242240] ['~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
[Mon Jul 03 00:58:21.619501 2017] [wsgi:error] [pid 4520:tid 139990843242240] PYTHON VERSION:
[Mon Jul 03 00:58:21.619539 2017] [wsgi:error] [pid 4520:tid 139990843242240] sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
[Mon Jul 03 00:58:21.619581 2017] [wsgi:error] [pid 4520:tid 139990843242240] PYTHON LOCATION OF EXECUTABLE:
[Mon Jul 03 00:58:21.619619 2017] [wsgi:error] [pid 4520:tid 139990843242240] /usr/bin/python
[Mon Jul 03 00:58:21.619646 2017] [wsgi:error] [pid 4520:tid 139990843242240]
[Mon Jul 03 00:58:21.619674 2017] [wsgi:error] [pid 4520:tid 139990843242240]
[Mon Jul 03 00:58:21.619806 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] mod_wsgi (pid=4520): Target WSGI script '/var/www/firstapp/hello.wsgi' cannot be loaded as Python module.
[Mon Jul 03 00:58:21.619844 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] mod_wsgi (pid=4520): Exception occurred processing WSGI script '/var/www/firstapp/hello.wsgi'.
[Mon Jul 03 00:58:21.619898 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] Traceback (most recent call last):
[Mon Jul 03 00:58:21.619952 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] File "/var/www/firstapp/hello.wsgi", line 4, in <module>
[Mon Jul 03 00:58:21.620010 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] from hello import app as application
[Mon Jul 03 00:58:21.620042 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] File "/var/www/firstapp/hello.py", line 10, in <module>
[Mon Jul 03 00:58:21.620082 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] from flask import Flask
[Mon Jul 03 00:58:21.620131 2017] [wsgi:error] [pid 4520:tid 139990843242240] [remote 100.38.132.195:60448] ImportError: No module named flask
[Mon Jul 03 00:58:40.176748 2017] [wsgi:error] [pid 4520:tid 139990725629696] PYTHON SYS.PATH:
[Mon Jul 03 00:58:40.176930 2017] [wsgi:error] [pid 4520:tid 139990725629696] ['~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '~/.local/lib/python2.7/site-packages', '/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
[Mon Jul 03 00:58:40.176983 2017] [wsgi:error] [pid 4520:tid 139990725629696] PYTHON VERSION:
[Mon Jul 03 00:58:40.177023 2017] [wsgi:error] [pid 4520:tid 139990725629696] sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
[Mon Jul 03 00:58:40.177060 2017] [wsgi:error] [pid 4520:tid 139990725629696] PYTHON LOCATION OF EXECUTABLE:
[Mon Jul 03 00:58:40.177092 2017] [wsgi:error] [pid 4520:tid 139990725629696] /usr/bin/python
[Mon Jul 03 00:58:40.177122 2017] [wsgi:error] [pid 4520:tid 139990725629696]
[Mon Jul 03 00:58:40.177150 2017] [wsgi:error] [pid 4520:tid 139990725629696]
[Mon Jul 03 00:58:40.177327 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] mod_wsgi (pid=4520): Target WSGI script '/var/www/firstapp/hello.wsgi' cannot be loaded as Python module.
[Mon Jul 03 00:58:40.177380 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] mod_wsgi (pid=4520): Exception occurred processing WSGI script '/var/www/firstapp/hello.wsgi'.
[Mon Jul 03 00:58:40.177430 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] Traceback (most recent call last):
[Mon Jul 03 00:58:40.177490 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] File "/var/www/firstapp/hello.wsgi", line 4, in <module>
[Mon Jul 03 00:58:40.177559 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] from hello import app as application
[Mon Jul 03 00:58:40.177604 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] File "/var/www/firstapp/hello.py", line 10, in <module>
[Mon Jul 03 00:58:40.177660 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] from flask import Flask
[Mon Jul 03 00:58:40.177709 2017] [wsgi:error] [pid 4520:tid 139990725629696] [remote 100.38.132.195:63520] ImportError: No module named flask
我可能弄错了,但我认为错误的重要部分是:
ImportError: No module named flask
奇怪的是,如果我运行“python hello.py”(文件和内容见下文),没有烧瓶导入错误。这告诉我,当 Apache 使用 mod_wsgi 执行 hello.py 时,它“忘记”了 Flask 在哪里。我不知道如何告诉它在哪里看。当我验证 Flask 已安装时,PIP 会指定它的安装位置。
alexanderblank@instance-1:/var/www/firstapp$ pip install flask
Requirement already satisfied: flask in /home/alexanderblank/.local/lib/python2.7/site-packages
Requirement already satisfied: itsdangerous>=0.21 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: click>=2.0 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: Werkzeug>=0.7 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: Jinja2>=2.4 in /home/alexanderblank/.local/lib/python2.7/site-packages (from flask)
Requirement already satisfied: MarkupSafe>=0.23 in /home/alexanderblank/.local/lib/python2.7/site-packages (from Jinja2>=2.4->flask)
我尝试将该文件夹“插入”到 WSGI 文件中的 Python 搜索路径中:
sys.path.insert(0, "~/.local/lib/python2.7/site-packages")
我已经详细说明了我在下面采取的具体步骤(服务器设置和代码文件),并且已经尝试解决这个问题几个小时了。
我在 Google Compute Engine 上创建了一个新的 Ubuntu 16 LTS 实例——我将其称为“VPS”(用于虚拟专用服务器)。我完成了以下步骤:
(1) SSH 进入 VPS。 (2) 运行以下命令安装python、pip和flask:
sudo apt-get update
sudo apt-get install python-pip
pip install --user Flask
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
(3) 通过访问 VPS 公网 IP 测试配置,它显示了默认的 Apache2 Ubuntu 页面(因此防火墙工作,Web 服务器似乎能够提供默认页面)。
然后我提取了我使用 GIT 在本地创建的 Python 文件。在 /var/www/ 我有以下文件:
instance-1:/var/www$ tree -R
.
├── firstapp
│ ├── hello.py
│ ├── hello.pyc
│ ├── hello.wsgi
│ └── README.md
└── html
└── index.html
在 /etc/apache2/sites-available 我创建了一个名为 hello.conf 的新文件
├── 000-default.conf
├── default-ssl.conf
└── hello.conf
我还递归地获得了 /etc/apache2 目录的所有权,使用:
sudo chown -R [my_ssh_username] /etc/apache2
然后我告诉 Apache 忽略旧的 .conf 文件,而是通过运行来查看 hello.conf:
sudo a2dissite 000-default.conf
sudo a2ensite hello.conf
sudo service apache2 reload
当我访问 VPS 的公共 IP 地址时,这会导致 500 Internal Server 错误。我通过运行查看了日志文件:
sudo tail -f /var/log/apache2/error.log
奇怪的是,从 SSH shell 运行文件按预期工作(导入烧瓶没有问题):
instance-1:/var/www/firstapp$ python hello.py
PYTHON SYS.PATH:
['/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/alexanderblank/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
PYTHON VERSION:
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
PYTHON LOCATION OF EXECUTABLE:
/usr/bin/python
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
PYTHON SYS.PATH:
['/var/www/firstapp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/alexanderblank/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
PYTHON VERSION:
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
PYTHON LOCATION OF EXECUTABLE:
/usr/bin/python
* Debugger is active!
* Debugger PIN: 278-244-316
我已将我制作的重要文件的内容包含在下面。
hello.conf
<VirtualHost *>
ServerName example.com
WSGIScriptAlias / /var/www/firstapp/hello.wsgi
WSGIDaemonProcess hello
<Directory /var/www/firstapp>
WSGIProcessGroup hello
WSGIApplicationGroup ${GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
你好.py
import sys
print "PYTHON SYS.PATH:"
print sys.path
print "PYTHON VERSION:"
print sys.version_info
print "PYTHON LOCATION OF EXECUTABLE:"
print sys.executable
print "\n"
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(port=5000, debug=True)
你好.wsgi
import sys
sys.path.insert(0, "/var/www/firstapp")
sys.path.insert(0, "~/.local/lib/python2.7/site-packages")
from hello import app as application
【问题讨论】:
标签: python apache flask google-compute-engine mod-wsgi