在 apache 上部署 Flask 应用程序时出现导入错误

在 apache 上部署 Flask 应用程序时出现导入错误

我正在尝试使用 WSGI 在 VPS 上部署 flask 应用程序。我没有使用虚拟环境,并且系统上安装了 pandas:

Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> print 'Works!'
Works!

我仍然收到此错误:

[Sat Jun 01 16:02:52.876184 2019] [wsgi:error] [pid 24086] [client 27.7.8.82:59860] mod_wsgi (pid=24086): Target WSGI script '/var/www/im/server.wsgi' cannot be loaded as Python module.
[Sat Jun 01 16:02:52.876217 2019] [wsgi:error] [pid 24086] [client 27.7.8.82:59860] mod_wsgi (pid=24086): Exception occurred processing WSGI script '/var/www/im/server.wsgi'.
[Sat Jun 01 16:02:52.876236 2019] [wsgi:error] [pid 24086] [client 27.7.8.82:59860] Traceback (most recent call last):
[Sat Jun 01 16:02:52.876254 2019] [wsgi:error] [pid 24086] [client 27.7.8.82:59860]   File "/var/www/im/server.wsgi", line 7, in <module>
[Sat Jun 01 16:02:52.876278 2019] [wsgi:error] [pid 24086] [client 27.7.8.82:59860]     from UI import app as application
[Sat Jun 01 16:02:52.876286 2019] [wsgi:error] [pid 24086] [client 27.7.8.82:59860]   File "/var/www/im/UI/__init__.py", line 3, in <module>
[Sat Jun 01 16:02:52.876297 2019] [wsgi:error] [pid 24086] [client 27.7.8.82:59860]     import pandas as pd
[Sat Jun 01 16:02:52.876315 2019] [wsgi:error] [pid 24086] [client 27.7.8.82:59860] ImportError: No module named pandas

这是 WSGI 文件:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/im/")

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

有人能指出哪里出了问题吗?我正在 Ubuntu 18.04.2 LTS 上尝试此操作。

答案1

很可能您的机器上还安装了一个 Python 解释器。要检查,请执行以下操作:

  • 从服务器控制台运行 python 并执行以下命令:

    import sys
    print sys.path
    
  • 创建测试 Flask 应用Create a test Flask app运行.py下面,将其放入/var/www/im/文件夹并编辑 WSGI 文件以加载它:from run import app as application

    import sys
    from flask import Flask
    app = Flask(__name__)
    
    @app.route('/')
    def get_path():
        return str(sys.path)
    
  • 比较两者的输出

相关内容