Apache-2.4

在 apache 上部署燒瓶應用程序時出現導入錯誤

  • June 17, 2019

我正在嘗試使用 WSGI 在 VPS 上部署燒瓶應用程序。我沒有使用 virtualenv,並且系統上安裝了 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 上嘗試這個。

很可能你在盒子上安裝了一個 Python 解釋器。要檢查,請執行以下操作:

  • 從伺服器控制台執行 python 並執行以下命令:
import sys
print sys.path
  • 在下面創建一個測試 Flask 應用程序run.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)
  • 比較兩者的輸出

引用自:https://serverfault.com/questions/969780