环境
Windows 10 (x64)
Anaconda 3 (64 bit)
初次打包需要下载一些依赖包,需要能够连接到github
准备
从以下官方源下载PyStand-py38-x64.7z:嵌入式 Python 环境 (64位)
https://github.com/skywind3000/PyStand/releases/tag/1.1.2
解压该文件可以发现runtime文件夹里面放python embeddable解释器,自带的是python3.8.10
当然也可以使用指定的python embeddable解释器。
去官网下载Windows embeddable package (64-bit)并替换runtime文件夹中的文件即可。
使用了指定版本的python解释器,比如Python3.11.9,下面虚拟环境也要使用一样的版本,否则会出现一系列报错。
https://www.python.org/downloads/release/python-3119/
配置python虚拟环境
这里基于上面自带的python3.8.10创建conda虚拟环境
conda create -n py3810_env python=3.8.10
参考《Anaconda-用conda创建python虚拟环境》
来自 https://zhuanlan.zhihu.com/p/94744929
安装Nuitka
进入python3.8.10虚拟环境
activate py3810_env
pip install nuitka -i https://mirrors.aliyun.com/pypi/simple/
打包
使用Nuitka打包第一个exe
打包第一个 exe , python -m nuitka --nofollow-imports --show-progress --standalone main.py,目的是完成打包环境的自动配置。
这里的main.py可以是任意的Python文件,或者仅仅是打印hello world都可以。
打包耗时会比较长,过程中会下载一些依赖包,如果遇到网络问题,可以单独下载放到对应的文件夹下。
正常结束后将打包生成的文件都删除即可。
使用前面下载的PyStand运行第一个Python文件example.py
import sys
import numpy as np
import pandas as pd
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=10, height=10, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot()
# 初始化父类
super(MplCanvas, self).__init__(fig)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# 创建MplCanvas实例作为窗口中的中央部件
self.canvas = MplCanvas(self, width=10, height=8, dpi=200)
# 生成数据并绘制
x_values = np.linspace(0, 2 * np.pi, num=200)
y_values = np.cos(x_values)
df = pd.DataFrame({'x': x_values, 'cos(x)': y_values})
self.canvas.axes.plot(x_values, y_values, label='Cosine Function')
# 设置图表标题、坐标轴标签等
self.canvas.axes.set_xlabel('X axis (x)')
self.canvas.axes.set_ylabel('Y axis(cos(x))')
self.canvas.axes.set_title('y = cos(x)')
self.canvas.axes.legend()
# 布局管理
layout = QVBoxLayout()
layout.addWidget(self.canvas)
self.text=QLabel(str(df.info))
layout.addWidget(self.text)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def start():
app = QApplication(sys.argv)
main_win = MainWindow()
main_win.show()
sys.exit(app.exec_())
将该example.py放置到PyStand.exe所在的文件夹下,然后更改该文件夹下的PyStand.int内容如下:
from example import start
if __name__ == "__main__":
#运行入口函数
start()
安装Python包
进入python虚拟环境,安装上述python文件中用到的依赖包
activate py3810_env
pip install numpy pandas PyQt5 matplotlib -i https://mirrors.aliyun.com/pypi/simple/
这里一定要用pip安装,用conda安装的不行,会报错。
安装完后,将虚拟环境下的sitepackage文件夹下所有内容copy到pystand的sitepackage文件夹下
文件路径参考:anaconda3\envs\py3810_env\Lib\site-packages
测试运行
双击PyStand.exe
加密源代码
进入对应的python虚拟环境,将example.py转换成pyd文件
activate py3810_env
python -m nuitka --module example.py
然后把example.py删除,有__pycache__文件夹的,把这个文件夹也一并删除。
再次双击PyStand.exe进行测试。
参考文档
怎么样打包 pyqt 应用才是最佳方案?或者说 pyqt 怎样的发布方式最优?
来自 https://www.zhihu.com/question/48776632/answer/2336654649
python嵌入式打包,打包新姿势
来自 https://blog.csdn.net/m0_65740074/article/details/137522165?spm=1001.2014.3001.5506
nuitka 打包使用配置文件的 exe
来自 https://blog.csdn.net/qq_17328759/article/details/120230311
Python 打包工具 Nuitka 入门指南
来自 https://blog.csdn.net/m0_59838087/article/details/129775997
Nuitka打包教程
来自 https://blog.51cto.com/u_14121041/6414698
Nuitka打包python代码教程
来自 https://blog.csdn.net/LEILEI18A/article/details/136397703