当前位置:AIGC资讯 > AIGC > 正文

项目包括whisper、pytorch、cuda、cudnn、chocolatey、ffmpeg、tkinterdnd2、tkinter、os、conda

项目包括whisper、pytorch、cuda、cudnn、chocolatey、ffmpeg、tkinterdnd2、tkinter、os、conda

希望将软件包分发给没有python,也没有打开cmd的能力的人使用。

安装chocolatey等系统级依赖,使用ps1脚本。

# 检查是否以管理员身份运行
function Test-Admin {
    $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = [System.Security.Principal.WindowsPrincipal]$identity
    return $principal.IsInRole([System.Security.Principal.WindowsBuiltinRole]::Administrator)
}

if (-not (Test-Admin)) {
    Write-Host "This script must be run as an administrator." -ForegroundColor Red
    Start-Process powershell -ArgumentList "Start-Process PowerShell -ArgumentList 'Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString(''https://community.chocolatey.org/install.ps1''))' -Verb RunAs" -Verb RunAs
    exit
}

# 确保 Chocolatey 已安装
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
    Write-Host "Chocolatey is not installed. Installing Chocolatey..." -ForegroundColor Yellow
    Set-ExecutionPolicy Bypass -Scope Process -Force
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
    iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}

# 安装 Python 3.12 和 pip 工具
Write-Host "Installing Python 3.12 and pip..." -ForegroundColor Green
choco install python --version=3.12 --confirm

# 确保 pip 工具已安装
Write-Host "Ensuring pip is installed..." -ForegroundColor Green
python -m ensurepip

# 安装 FFmpeg
Write-Host "Installing FFmpeg..." -ForegroundColor Green
choco install ffmpeg --confirm

Write-Host "Installation complete. Python, pip, and FFmpeg should now be installed." -ForegroundColor Green
 

如果报错

警告: Files from a previous installation of Chocolatey were found at 'C:\ProgramData\chocolatey'. 警告: An existing Chocolatey installation was detected. Installation will not continue. This script will not overwrite existing installations. If there is no Chocolatey installation at '', delete the folder and attempt the installation again. Please use choco upgrade chocolatey to handle upgrades of Chocolatey itself. If the existing installation is not functional or a prior installation did not complete, follow these steps: - Backup the files at the path listed above so you can restore your previous installation if needed. - Remove the existing installation manually. - Rerun this installation script. - Reinstall any packages previously installed, if needed (refer to the lib folder in the backup). Once installation is completed, the backup folder is no longer needed and can be deleted.

解决办法

删除现有的 Chocolatey 安装

删除 C:\ProgramData\chocolatey 目录:

打开文件资源管理器,导航到 C:\ProgramData。 找到 chocolatey 文件夹并删除。

从环境变量中删除 Chocolatey 的路径(共有三处):

右键单击“此电脑”或“计算机”,选择“属性”。 选择“高级系统设置”。 在“系统属性”对话框中,选择“环境变量”。 在“系统变量”部分找到“Path”变量并编辑。 删除路径中包含 Chocolatey 的条目。 找到并删除另外两处变量名含有Chocolatey 的变量。

在代码文件Translate.py所在文件夹的路径中输入cmd

为了打包方便,创建虚拟环境,并进入

conda create whisper

conda activate whisper

使用pyinstaller打包,命令

pyinstaller --onefile --noconsole --paths "D:/ProgramData/anaconda3/envs/whisper/Lib/site-packages" --add-data "D:/ProgramData/anaconda3/envs/whisper/Lib/site-packages/tkinterdnd2;tkinterdnd2" --add-data "D:/ProgramData/anaconda3/envs/whisper/Lib/site-packages/tkinterdnd2/tkdnd/win-x64/tkdnd.tcl;tkdnd" --hidden-import=tkinterdnd2 --i 1.ico --clean Translate.py

此处感谢博主提供解决tkdnd无法打包问题的命令模板。可将模板告诉GPT,再用everything找到本地tkinterdnd2所在路径并告诉GPT,让它参考模板,但是用我提供的路径生成命令https://blog.csdn.net/qq_43079386/article/details/139725149

设置icon使用参数--i name.ico如--i cat.ico。 icon大小不得超过256*256px,可以使用photopee编辑。https://jingyan.baidu.com/article/fedf0737978d8835ad897770.html

到此,代码本体、依赖库打包完毕,系统级依赖自动安装脚本准备完毕。

接下来尝试用安装器封装一下,安装器要实现自动运行系统级依赖自动安装脚本install_dependencies.ps1,给将打包好的Translate.exe增加右键菜单和任务栏快捷方式

使用Inno Setup制作程序安装包-CSDN博客

总结

### 文章总结
本文描述了一个复杂的项目配置与打包流程,旨在将一系列包含Python及系统级依赖的软件包(如whisper、PyTorch、CUDA、cuDNN、Chocolatey、FFmpeg等)分发给不具备直接Python环境安装及cmd命令执行能力的终端用户。
#### 主要步骤包括:
1. **安装系统级依赖**:
- 使用PowerShell脚本(`.ps1`)检查并安装Chocolatey,这是一个Windows包管理器,用于简化软件包的安装和管理。
- 脚本中检查了是否以管理员身份运行,并确保CHOCOLATEY已安装,若未安装则自动从官方源下载安装。
- 使用Chocolatey安装Python 3.12及pip、FFmpeg等依赖。
2. **处理潜在的安装问题**:
- 针对已存在Chocolatey安装但可能需要更新的情况,提供了详细的手动删除和重装步骤。
3. **Python环境配置与打包**:
- 使用Conda创建虚拟环境并激活,以隔离项目依赖。
- 使用`pyinstaller`工具打包Python脚本及其依赖,特别处理了`tkinterdnd2`模块的打包问题,并包含了`tkdnd`组件。
- 指导了如何为打包的应用添加图标,并强调了图标大小要求及处理方法。
4. **提高用户友好度**:
- 考虑为最终用户提供安装器,如使用Inno Setup制作安装包,该安装器能够自动运行系统级依赖安装脚本,并为打包好的应用添加右键菜单和任务栏快捷方式,以提升用户体验。
#### 关键工具与技术:
- **Chocolatey**:Windows下的包管理器,用于自动化安装各类软件包。
- **PowerShell**(`.ps1`脚本):自动化脚本语言,用于管理Windows环境。
- **Conda**:科学计算环境中的包与环境管理器。
- **PyInstaller**:将Python项目打包成独立可执行文件的工具。
- **Inno Setup**:免费的Windows安装程序制作软件。
#### 注意事项:
- 确保在执行安装脚本时拥有足够的权限(通常需要以管理员身份运行)。
- 打包时需要注意路径、依赖以及模块的特殊处理(如`tkinterdnd2`)。
- 为最终用户提供的安装包应具备自动配置和尽量少的用户干预要求。
#### 展望:
- 未来可以进一步完善安装流程,确保在各种Windows配置下都能顺利运行。
- 优化用户体验,例如提供更详细的日志输出和用户文档。
- 探索将更多系统级依赖直接集成到安装包中,减少用户外部依赖。

更新时间 2024-09-24