1、CentOS7系统现有python版本

我们先来查看一下安装的CentOS 7操作系统中python的版本情况。Centos7系统目前自带python的版本为2.7.5。

[root@qinglong ~]# python -V
Python 2.7.5

使用linux命令whereis python来查看python安装路径:

[root@qinglong ~]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/bin/python3.6 /usr/bin/python3.6m /usr/lib/python2.7 /usr/lib/python3.6 /usr/lib64/python2.7 /usr/lib64/python3.6 /etc/python /usr/local/lib/python3.6 /usr/include/python2.7 /usr/include/python3.6m /usr/share/man/man1/python.1.gz

2、python安装

python的安装有2种方式,具备上网条件的可以使用yum install python3进行安装,不具备上网条件的可以使用编译安装。

#1.方式一 yum安装
[root@qinglong ~]# yum install python3
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base  省略  
软件包 python3-3.6.8-18.el7.x86_64 已安装并且是最新版本
无须任何处理


#2.方式二 下载编译安装
#下载地址,可以使用ftp或者wget下载
wget https://link.zhihu.com/?target=https%3A//www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz

#3.将下载的安装包上传至服务器目录/tmp目录
#4.解压缩安装包
[root@qinglong tmp]#tar -zxf Python-3.7.2.tgz
[root@qinglong tmp]ll
total 22372
-rw-------.  1 root root     1431 Jun 10 21:05 anaconda-ks.cfg
drwxr-xr-x. 18  501  501     4096 Dec 24  2018 Python-3.7.2
#5.进入解压目录进行配置
[root@qinglong Python-3.7.2]./configure --with-ssl

#6.执行编译和安装
[root@qinglong Python-3.7.2]make
[root@qinglong Python-3.7.2]make install

3、Python3配置连接

#1.用whereis来查看python3通过编译安装后存放的路径,以yum安装为例子。可以看出都在/usr/bin下
[root@qinglong ~]# whereis python3
python3: /usr/bin/python3 /usr/bin/python3.6 /usr/bin/python3.6m /usr/lib/python3.6 /usr/lib64/python3.6 /usr/local/lib/python3.6 /usr/include/python3.6m /usr/share/man/man1/python3.1.gz

#2.进入/usr/bin目录先删除原有指向python2的软连接python,然后将python3.7与python建立软连接。
[root@qinglong ~]# cd /usr/bin/
[root@qinglong bin]# pwd
/usr/bin

#3.备份旧的python执行程序
[root@qinglong bin]# ll python*
lrwxrwxrwx. 1 root root     7 12月 18 2021 python -> python2
lrwxrwxrwx. 1 root root     9 12月 18 2021 python2 -> python2.7
-rwxr-xr-x. 1 root root  7144 10月 14 2020 python2.7
lrwxrwxrwx. 1 root root     9 1月   6 2022 python3 -> python3.6
-rwxr-xr-x. 2 root root 11328 11月 17 2020 python3.6
-rwxr-xr-x. 2 root root 11328 11月 17 2020 python3.6m
[root@qinglong bin]# mv python python.bak
[root@qinglong bin]# 

#4.添加python3.6的软连接
[root@qinglong bin]# ln -s /usr/bin/python3.6 /usr/bin/python
[root@qinglong bin]# ll python*
lrwxrwxrwx. 1 root root    18 10月 14 16:54 python -> /usr/bin/python3.6
lrwxrwxrwx. 1 root root     9 12月 18 2021 python2 -> python2.7
-rwxr-xr-x. 1 root root  7144 10月 14 2020 python2.7
lrwxrwxrwx. 1 root root     9 1月   6 2022 python3 -> python3.6
-rwxr-xr-x. 2 root root 11328 11月 17 2020 python3.6
-rwxr-xr-x. 2 root root 11328 11月 17 2020 python3.6m
lrwxrwxrwx. 1 root root     7 12月 18 2021 python.bak -> python2
[root@qinglong bin]# 

#5.验证python版本
[root@qinglong bin]# python -V
Python 3.6.8

4、解决yum安装解析问题

因为yum采用python作为命令解释器,现在设置了python3.7为/usr/bin/python的软连接,因此再运行yum时会报错:

[root@qinglong bin]# yum
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

#进入root账户,编辑 vi /usr/bin/yum
将第一行语句#!/usr/bin/python 修改为 #!/usr/bin/python2.7
同时修改urlgrabber-ext-down 文件即: vi /usr/libexec/urlgrabber-ext-down,同样将第一行语句#!/usr/bin/python 修改为 #!/usr/bin/python2.7即可。

5、pip工具无法使用的问题

yum install epel-release
yum install -y python-pip

6、安装openCV服务器

#1. 检查服务器是否有python环境,(python -V 或者 python3 -V,后面以python3为例)。

#2. 下载ccb_opencv.py 接口服务脚本到服务器,(比如目录:/root/ccb/ccb_opencv.py)。

#3. 切换到脚本目录(cd /root/ccb)。
[root@qinglong ~]# mkdir ccb
[root@qinglong ~]# cd ccb
#4. 先手动运行,看是否缺少依赖(python3 ccb_opencv.py)。

#5. 缺少依赖的,运行(pip 或 pip3 install Flask opencv-python aircv), 安装后再走第4步。
pip install Flask opencv-python aircv -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
语法解析:
pip install 想要安装的包 -i 指定国内源(我在这里用的是清华的,时间是2020年12月13日)
pip install Flask -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install pbr -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

[root@qinglong ccb]# pip install Flask opencv-python -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting Flask
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/cd/77/59df23681f4fd19b7cbbb5e92484d46ad587554f5d490f33ef907e456132/Flask-2.0.3-py3-none-any.whl (95 kB)
     |████████████████████████████████| 95 kB 902 kB/s             
Collecting opencv-python
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/af/bf/8d189a5c43460f6b5c8eb81ead8732e94b9f73ef8d9abba9e8f5a61a6531/opencv_python-4.6.0.66-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.9 MB)
     |████████████████████████████████| 60.9 MB 8.3 MB/s             
Collecting aircv
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bd/70/fdf23058fb0728f5d241287562e088b1b4d5d9e68c6689b1afbbc53ec10b/aircv-1.4.6.tar.gz (4.0 MB)
     |████████████████████████████████| 4.0 MB 20.1 MB/s            
  Preparing metadata (setup.py) ... -

[root@qinglong ccb]# pip install pbr -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting pbr
  Downloading http://pypi.doubanio.com/packages/88/fb/c7958b2d571c7b15091b8574a727ad14328e8de590644198e57de9b5ee57/pbr-5.10.0-py2.py3-none-any.whl (112 kB)
     |████████████████████████████████| 112 kB 3.4 MB/s            
Installing collected packages: pbr
Successfully installed pbr-5.10.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[root@qinglong ccb]# pip install aircv -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting aircv
  Downloading http://pypi.doubanio.com/packages/bd/70/fdf23058fb0728f5d241287562e088b1b4d5d9e68c6689b1afbbc53ec10b/aircv-1.4.6.tar.gz (4.0 MB)
     |████████████████████████████████| 4.0 MB 4.1 MB/s            
  Preparing metadata (setup.py) ... done
Using legacy 'setup.py install' for aircv, since package 'wheel' is not installed.
Installing collected packages: aircv
    Running setup.py install for aircv ... done
Successfully installed aircv-1.4.6
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

#创建日志目录
[root@qinglong ccb]# mkdir -p /root/ccb/logs/
#测试运行
[root@qinglong ccb]# python ccb_opencv.py 
 * Serving Flask app 'ccb_opencv' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
WARNING:werkzeug: * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
INFO:werkzeug: * Running on http://192.168.0.128:8998/ (Press CTRL+C to quit)


6. 手动运行成功后,在青龙环境中配置OPENCV接口服务地址(局域网IP,或者公网IP),然后青龙运行ccb脚本,注意拼图活动日志,测试服务是否正常 。

7. 测试正常后,运行 nohup python3 ccb_opencv.py > logcv.txt 2>&1 & 。

8. 日志在logcv.txt中查看 。

9. ccb_opencv.py 接口服务脚本下载地址:https://gitee.com/maxinDev/ccb-boot/blob/master/ccb_opencv.py

10. 青龙面板配置文件
## 建设银行
#J脚本更新地址
export CCB_PY_SERVER="http://43.143.30.70:5000/ccb/updatePy"
#CCB自动抽奖控制开关:  1 自动抽奖
export CCB_AUTO_CHOU="1"
#CCB 拼图有限 需要调用的远程OPENCV服务器地址
export CCB_OPENCV_URL="http://collick.cn:8998/"


最后修改:2022 年 11 月 21 日
如果觉得我的文章对你有用,请随意赞赏