王海庆的云笔记

CentOS7上面搭建Python2+PyQt4


关闭防火墙

systemctl stop firewalld
systemctl disable firewalld
sed -i -e  's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0


换成阿里云的源

rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum clean all
yum makecache fast -y


公司里CentOS7的源

rm -rf /etc/yum.repos.d/*
cat >  /etc/yum.repos.d/centos7.repo << EOF
[base]
name=base
baseurl=http://172.18.0.61/centos7/base
enabled=1
gpgcheck=0

[extras]
name=extras
baseurl=http://172.18.0.61/centos7/extras
enabled=1
gpgcheck=0

[updates]
name=updates
baseurl=http://172.18.0.61/centos7/updates
enabled=1
gpgcheck=0

[epel]
name=epel
baseurl=http://172.18.0.61/centos7/epel
enabled=1
gpgcheck=0
EOF

yum clean all
yum makecache fast -y

 

安装pip

yum install python-pip -y


更新pip,不能安装最新的

pip install --upgrade pip==20.3.4 -i https://mirrors.aliyun.com/pypi/simple/
pip install --upgrade pip==20.3.4 -i http://172.18.0.69:8081/repository/pip-group/simple/ --trusted-host 172.18.0.69


安装sip和PyQt4 

yum install -y PyQt4 PyQt4-devel


安装Pyinstaller

pip install pyinstaller==3.6 -i http://172.18.0.69:8081/repository/pip-group/simple/ --trusted-host 172.18.0.69


运行代码

# 源码运行
cd /root/zbatch-gui
python main.py

# 源码打包
cd /root/zbatch-gui
pyinstaller main.py --clean -y

# 二进制运行
/root/zbatch-gui/dist/main/main


如果Python2采用QString2

[root@zkxy zbatch-gui]# /root/zbatch-gui/dist/main/main
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    from plugin.chart import PieView  #不要放在别的后面
  File "/usr/lib/python2.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 391, in load_module
    exec(bytecode, module.__dict__)
  File "plugin/chart.py", line 4, in <module>
    sip.setapi('QString', 2)
ValueError: API 'QString' has already been set to version 1
[3113] Failed to execute script main
[root@zkxy zbatch-gui]#


创建hook文件

cat > rthook_pyqt4.py << EOF
import sip

sip.setapi(u'QDate', 2)
sip.setapi(u'QDateTime', 2)
sip.setapi(u'QString', 2)
sip.setapi(u'QTextStream', 2)
sip.setapi(u'QTime', 2)
sip.setapi(u'QUrl', 2)
sip.setapi(u'QVariant', 2)
EOF


重新打包

# 源码打包
cd /root/zbatch-gui
pyinstaller main.py --runtime-hook rthook_pyqt4.py --clean -y

# 二进制运行
/root/zbatch-gui/dist/main/main


备注

使用PyQt4自带的sip模块,能够任意的选择2.x和3.x风格的QString实现。命令是

import sip

sip.setapi('QString',1) # 1 = Python 2.x; 2 = Python 3.x


参考

https://www.jianshu.com/p/6e0d3a66a722

文章最后更新时间: 2022-06-16 17:32:03