创建个人网站多少钱,微信二维码制作小程序,阿里云云栖wordpress,军事新闻最新消息今天PyInstaller 是一个用于将 Python 程序打包成独立可执行文件的工具#xff0c;支持 Windows、Linux 和 macOS。1、安装PyInstallerpip install pyinstaller2、使用方法2.1 简单应用# 基本打包命令
pyinstaller your_script.py# 打包成单个文件
pyinstaller -F your_script.py#…PyInstaller 是一个用于将 Python 程序打包成独立可执行文件的工具支持 Windows、Linux 和 macOS。1、安装PyInstallerpipinstallpyinstaller2、使用方法2.1 简单应用# 基本打包命令 pyinstaller your_script.py # 打包成单个文件 pyinstaller -F your_script.py # 打包成单个文件不显示控制台窗口适合GUI程序 pyinstaller -F -w your_script.py2.2常用参数说明参数说明示例-F, --onefile打包成单个可执行文件pyinstaller -F app.py-D, --onedir打包成一个文件夹默认pyinstaller -D app.py-w, --windowed不显示控制台窗口pyinstaller -w app.py-c, --console显示控制台窗口默认pyinstaller -c app.py-n NAME, --name NAME指定生成文件名pyinstaller -n MyApp app.py-i ICON, --icon ICON设置程序图标pyinstaller -i icon.ico app.py--add-data SRC:DST添加额外文件--add-data data/*:data--add-binary SRC:DST添加二进制文件--add-binary lib/*:lib--hidden-import MODULE添加隐藏导入的模块--hidden-import pandas--exclude-module MODULE排除不需要的模块--exclude-module matplotlib--clean清理临时文件pyinstaller --clean app.py--upx-dir DIR指定 UPX 压缩工具目录--upx-dir C:\upx-y, --noconfirm覆盖输出目录不确认pyinstaller -y app.py2.3简单示例示例一基本打包demo.py文件内容为“print(你好)”使用命令“pyinstaller demo.py”进行打包打包成功会显示“successfully”。打包后会生成build、dist文件夹spec文件。spec文件为配置文件配置文件可以修改修改会影响后续打包。build为打包时的临时构建文件打包后可以删除没有影响。dist为可执行文件所在目录存放exe可执行文件及各种依赖文件。将dist下整个demo文件夹复制到没有python环境的设备上也可以运行。点击shift鼠标右键打开powershell窗口运行demo.exe结果如下示例二带图标的单个可执行文件demo.py文件同路径下放ico图片使用命令“pyinstaller -F -i icon.ico demo.py”进行打包打包后如下图只有一个可执行文件。2.4 常见问题2.4.1添加隐藏导入pyinstaller --hidden-importpandas --hidden-importnumpy app.py2.4.2排除不需要的模块pyinstaller --exclude-modulematplotlib --exclude-modulescipy app.py2.4.3路径处理程序打包后在运行时会解压到系统临时目录如 sys._MEIPASS。如果直接用相对路径找资源打包后会失效打所以需要对路径进行处理。import sys import os def resource_path(relative_path): 获取资源文件的绝对路径 try: # PyInstaller 打包后的程序运行时会自动创建一个临时目录存放解压后的资源该目录的路径会被赋值给 sys._MEIPASS # 如果能够获取到sys._MEIPASS说明程序是打包后的可执行文件 base_path sys._MEIPASS except AttributeError: # 若获取不到sys._MEIPASS会触发异常然后获取源码所在的绝对目录 base_path os.path.abspath(.) # 拼接接待路径并返回 return os.path.join(base_path, relative_path)3、seleniumpython程序打包selenium程序打包时需要注意driver文件。有三种方案方案核心特点适用场景驱动打进exe内单文件分发无需额外文件便携性最高驱动版本不同需要重新打包需离线运行、分发简单的场景驱动放在exe外部可灵活更换驱动版本无需重新打包需频繁调整驱动版本的测试场景Selenium自动下载无需手动管理驱动代码最简洁联网下载有时候不成功目前还未解决该问题联网环境、Selenium ≥4.6.0 的场景3.1将driver文件打包到程序包中举例打开百度网页。baidu.py文件中的代码为import os import sys import time from selenium import webdriver from selenium.webdriver.chrome.service import Service def get_chromedriver_path(): # 判断是否是打包后的exe运行环境 if getattr(sys, frozen, False): # 打包后exe解压的临时目录_MEIxxxxxx base_path getattr(sys, _MEIPASS, os.path.dirname(os.path.abspath(__file__))) else: # 开发环境当前文件所在目录将chromedriver.exe和baidu.py放同一个目录下了 base_path os.path.dirname(os.path.abspath(__file__)) # 拼接驱动路径无论哪种环境都找 base_path 下的 chromedriver.exe driver_path os.path.join(base_path, chromedriver.exe) return driver_path driver_path get_chromedriver_path() # 初始化 driver service Service(executable_pathdriver_path) options webdriver.ChromeOptions() driver webdriver.Chrome(serviceservice, optionsoptions) # 打开一个网站这里以百度为例 driver.get(https://www.baidu.com) driver.maximize_window() time.sleep(5) # 最后关闭驱动 driver.quit()在baidu.py所在目录使用命令 pyinstaller -F --add-binary chromedriver.exe;. baidu.py 进行打包。也可以用全路径打包pyinstaller -F --add-binary G:\test\chromedriver.exe;. G:\test\baidu.py直接运行打包生成的exe文件即可。3.2将driver文件放在打包软件的文件夹下举例打开百度网页。baidu.py文件中的代码为def get_chromedriver_path(): if getattr(sys, frozen, False): # 打包后读取 exe 所在目录的 chromedriver.exe而非临时目录 exe_dir os.path.dirname(sys.executable) # exe 所在文件夹路径 driver_path os.path.join(exe_dir, chromedriver.exe) else: # 开发环境读取当前文件目录的驱动 driver_path os.path.join(os.path.dirname(os.path.abspath(__file__)), chromedriver.exe) return driver_path driver_path get_chromedriver_path() # 初始化 driver service Service(executable_pathdriver_path) options webdriver.ChromeOptions() driver webdriver.Chrome(serviceservice, optionsoptions) # 打开一个网站这里以百度为例 driver.get(https://www.baidu.com) driver.maximize_window() time.sleep(5) # 最后关闭驱动 driver.quit()在baidu.py所在目录使用命令 pyinstaller -F -w baidu.py 进行打包。也可以用全路径打包pyinstaller -F -w G:\test\testcase\baidu.py将chromedriver.exe放在打包生成的exe文件同一目录下在运行exe文件即可。3.3自动下载驱动当Selenium 版本 ≥4.6.0可放弃手动管理驱动让Selenium自动下载匹配版本的驱动。例import time from selenium import webdriver from selenium.webdriver.chrome.options import Options # 配置 Chrome 选项 options Options() # 屏蔽无关日志可选 options.add_experimental_option(excludeSwitches, [enable-logging]) # 核心Selenium 4.6 自动管理驱动无需传 Service 和驱动路径 driver webdriver.Chrome(optionsoptions) driver.maximize_window() driver.get(https://www.baidu.com) time.sleep(10) driver.quit()可使用命令 pyinstaller -F -w G:\test\testcase\baidu.py 打包。