問題描述 伺服器鏡像有寶塔面板 + Python2,用yum和pip3安裝Python3 + Flask + Gunicorn後,寶塔面板打不開了。報錯如下: 報錯早就刷上去了,找不回來,這段照搬別人的[^引用1],因為看著差不了多少。 其實我的情況跟引用1差不多,但是就是最後一步依然報錯。 修複 ...
問題描述
伺服器鏡像有寶塔面板 + Python2,用yum和pip3安裝Python3 + Flask + Gunicorn後,寶塔面板打不開了。報錯如下:
Starting Bt-Panel..........failed
------------------------------------------------------
[Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/gunicorn/util.py", line 135, in load_class
mod = import_module('.'.join(components))
File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'geventwebsocket'
]
------------------------------------------------------
Error: BT-Panel service startup failed.
done
報錯早就刷上去了,找不回來,這段照搬別人的[^引用1],因為看著差不了多少。
其實我的情況跟引用1差不多,但是就是最後一步依然報錯。
修複
修複目標是讓寶塔面板和Python3都能正常使用。
01 | 卸載python3+修複軟鏈接
聽說寶塔面板使用Python2編寫,直接安裝Python3的話會出現問題。[^引用4]
卸載Python3之後依然報如上的錯,路徑居然依然是python3.6,猜測可能是軟鏈接接錯了。
[root@server bin]# ll | grep python
lrwxrwxrwx 1 root root 7 Jul 11 2019 python -> python2
lrwxrwxrwx 1 root root 9 Jul 11 2019 python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Jun 21 2019 python2.7
-rwxr-xr-x 1 root root 1835 Jun 21 2019 python2.7-config
lrwxrwxrwx 1 root root 16 Jul 11 2019 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 Jul 11 2019 python-config -> python2-config
雖然看著好像沒錯,但還是按照官方論壇里的方法[^引用2]操作一遍。
[root@server bin]# ll | grep python
lrwxrwxrwx 1 root root 18 Mar 2 17:01 python -> /usr/bin/python2.7
-rwxr-xr-x 1 root root 7216 Jun 21 2019 python2.7
-rwxr-xr-x 1 root root 1835 Jun 21 2019 python2.7-config
lrwxrwxrwx 1 root root 16 Jul 11 2019 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 Jul 11 2019 python-config -> python2-config
02 | 再次啟動,路徑正常了但仍報錯
Error: Not a string: ['0.0.0.0:8888\n']
.........failed
------------------------------------------------------
from BTPanel import app,sys,public
File "/usr/lib64/python2.7/site-packages/gevent/builtins.py", line 96, in __import__
result = _import(*args, **kwargs)
File "/www/server/panel/BTPanel/__init__.py", line 28, in <module>
from flask_sockets import Sockets
File "/usr/lib64/python2.7/site-packages/gevent/builtins.py", line 96, in __import__
result = _import(*args, **kwargs)
File "class/flask_sockets.py", line 11, in <module>
from geventwebsocket.gunicorn.workers import GeventWebSocketWorker as Worker
File "/usr/lib64/python2.7/site-packages/gevent/builtins.py", line 96, in __import__
result = _import(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/geventwebsocket/gunicorn/workers.py", line 2, in <module>
from gunicorn.workers.ggevent import GeventPyWSGIWorker
File "/usr/lib64/python2.7/site-packages/gevent/builtins.py", line 96, in __import__
result = _import(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 39, in <module>
class GeventWorker(AsyncWorker):
File "/usr/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 90, in GeventWorker
if hasattr(gevent.core, 'dns_shutdown'):
AttributeError: 'module' object has no attribute 'core'
------------------------------------------------------
Error: BT-Panel service startup failed.
03 | 根據引用1重裝gunicorn
報錯
ERROR: Package 'gunicorn' requires a different Python: 2.7.5 not in '>=3.4'
不明白為什麼使用python2的pip安裝會出現這個錯誤,但還是找到瞭解決方法[^引用3]:
[root@server bin]# pip install gunicorn==999
[root@server bin]# pip install gunicorn==0.14.1
先指定安裝一個不存在的版本999,回車後會提示可以安裝的版本,然後隨便挑一個安裝就好了,我挑了偏中間的0.14.1安裝。
04 | 重裝gunicorn後報錯依舊
到這步就沒轍了,甚至更新過寶塔面板也依然沒用。耗了很久,心態爆炸。
看了很多資料,看見有人去分析過寶塔面板的源碼去糾錯,於是我也找到了寶塔面板的開源地址,無意間看到源代碼根目錄下有一份requirements.txt
(pip freeze導出的需要的庫的名單和版本,見引用5),發現裡面根本就沒有gunicorn,人家用的是gevent!
嗯,心態爆炸了。
卸載掉gunicorn,下一刻就啟動起來了……
簡單測試過後,一點貓餅都沒有。
參考和引用
- lib.im - 解決寶塔面板升級後因Python2.7與Python3.6共存啟動失敗
- 寶塔論壇 - 解決升級Python,重啟主機後寶塔不能啟動
- CSDN - ERROR: Package 'gunicorn' requires a different Python: 2.7.5 not in '>=3.4'
- 主機貼士 - 在寶塔面板python2的基礎上安裝python3
- GitHub - BaoTa/requirements.txt
- 主機貼士 - 在寶塔面板python2的基礎上安裝python3