win64系统安装32位的python解释器和打包成exe程序

177 阅读2分钟

在win64操作系统中,当我们用python调用32位的dll动态库时,必须使用32位的python解释器,否则会报错,因此本文首先说明了在win64系统环境下,如何安装32位的python解释器,同时对32位虚拟环境下运行的python程序进行打包,将其打包封装成可执行的exe程序,便于移植到其他Windows系统使用,供大家参考。

1、查看操作系统位数

运行conda info查看操作系统位数,可以看到platform显示平台为win-64,即Windows64位操作系统

image.png

2、切换到win-32位模式

输入set CONDA_FORCE_32BIT=1命令

image.png

再次查看当前的环境信息

conda info

image.png

image.png

3、创建win-32操作系统pyqt_py39_32虚拟环境

conda create -n pyqt_py39_32 python=3.9

image.png

4、激活pyqt_py39_32虚拟环境,安装pyqt相关工具

image.png

5、安装pyinstaller打包工具

image.png 6、打包

首先先切换到项目根目录

image.png

然后运行pyinstaller -F Test.py命令进行打包

image.png

运行结束后,在项目目录下会生成一个dist目录文件夹,里面有一个.exe程序,双击运行即可。

image.png

7、双击运行Test.exe程序

image.png

然后点击click,显示结果

image.png

8、如果希望打包的exe程序,不需要要控制台,在打包命令中添加--noconsole

pyinstaller -F Test.py --noconsole

image.png

打包完成后,在dist目录下面,双击运行Test.exe程序,可以看到没有控制台了。

image.png

9、附Test.py代码

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QTextEdit
from PyQt5.QtCore import Qt
import ctypes

class Test(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('pyinstaller测试')

        self.button = QPushButton('click', self)
        self.button.clicked.connect(self.get_status)

        self.status_text = QTextEdit(self)
        self.status_text.setReadOnly(True)

        layout = QVBoxLayout()
        layout.addWidget(self.button)
        layout.addWidget(self.status_text)
        self.setLayout(layout)

        self.show()

    def get_status(self):
        status_message = "Hello World"
        self.status_text.setText(f"pyinstaller demo:{status_message}")


def run_app():
    app = QApplication(sys.argv)
    window = Test()
    sys.exit(app.exec_())


if __name__ == '__main__':
    run_app()

本文完毕,感谢阅读,希望对您有帮助!