• [CentOS] 宝塔面板与Python3的恩怨情仇


    问题描述

    服务器镜像有宝塔面板 + 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
    ']
    .........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,下一刻就启动起来了……

    简单测试过后,一点猫饼都没有。

    参考和引用

    1. lib.im - 解决宝塔面板升级后因Python2.7与Python3.6共存启动失败
    2. 宝塔论坛 - 解决升级Python,重启主机后宝塔不能启动
    3. CSDN - ERROR: Package 'gunicorn' requires a different Python: 2.7.5 not in '>=3.4'
    4. 主机贴士 - 在宝塔面板python2的基础上安装python3
    5. GitHub - BaoTa/requirements.txt
    6. 主机贴士 - 在宝塔面板python2的基础上安装python3
    本文为博主原创,禁止转载或摘编。
  • 相关阅读:
    MFC中的模态对话框与非模态对话框
    汇编学习:float与double速度问题
    汇编学习:二维数组遍历
    SSE练习:单精度浮点数组求和
    StretchDIBits速度测试(HALFTONE)
    StretchDIBits速度测试(COLORONCOLOR)
    多媒体定时器
    typedef与复杂声明
    Image Based Lighting In UE3
    One simple WPF & C# RayTracer
  • 原文地址:https://www.cnblogs.com/aixcyi/p/12398390.html
Copyright © 2020-2023  润新知