PCM_Report/force_cleanup.ps1

125 lines
3.9 KiB
PowerShell
Raw Normal View History

2025-12-11 14:32:31 +08:00
# 强力清理脚本 - 解决DLL锁定问题
Write-Host "强力清理 - 解决DLL锁定问题" -ForegroundColor Red
Write-Host "=" * 50
# 1. 强制终止所有相关进程
Write-Host "1. 强制终止相关进程..." -ForegroundColor Yellow
$processesToKill = @("python", "pythonw", "docx_creator", "Qt*", "PySide*")
foreach ($proc in $processesToKill) {
Get-Process -Name $proc -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
}
Start-Sleep -Seconds 3
# 2. 使用robocopy强制清理.venv目录
Write-Host "2. 使用robocopy强制清理..." -ForegroundColor Yellow
if (Test-Path .\.venv) {
# 创建空的临时目录
$emptyDir = New-TemporaryFile | ForEach-Object {
Remove-Item $_ -Force
New-Item -ItemType Directory -Path $_ -Force
return $_
}
# 使用robocopy镜像空目录到.venv相当于清空
robocopy $emptyDir .\.venv /MIR /NFL /NDL /NJH /NJS /NC /NS /NP
# 删除临时目录
Remove-Item $emptyDir -Force
# 最后删除.venv目录
Remove-Item .\.venv -Force -ErrorAction SilentlyContinue
Write-Host "已使用robocopy清理.venv" -ForegroundColor Green
}
# 3. 清理其他构建文件
Write-Host "3. 清理构建文件..." -ForegroundColor Yellow
$dirsToClean = @("build", "dist", "__pycache__", "*.spec")
foreach ($item in $dirsToClean) {
if (Test-Path $item) {
Remove-Item $item -Recurse -Force -ErrorAction SilentlyContinue
}
}
# 4. 清理Python缓存
Write-Host "4. 清理Python缓存..." -ForegroundColor Yellow
Get-ChildItem -Path . -Name "__pycache__" -Recurse -Directory | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path . -Name "*.pyc" -Recurse -File | Remove-Item -Force -ErrorAction SilentlyContinue
# 5. 重新创建虚拟环境
Write-Host "5. 重新创建虚拟环境..." -ForegroundColor Yellow
python -m venv .venv --clear
# 检查是否创建成功
if (Test-Path .\.venv\Scripts\Activate.ps1) {
Write-Host "✅ 虚拟环境创建成功" -ForegroundColor Green
} else {
Write-Host "❌ 虚拟环境创建失败" -ForegroundColor Red
Write-Host "尝试使用系统Python..." -ForegroundColor Yellow
# 尝试使用系统Python
py -m venv .venv --clear
if (Test-Path .\.venv\Scripts\Activate.ps1) {
Write-Host "✅ 使用系统Python创建成功" -ForegroundColor Green
} else {
Write-Host "❌ 虚拟环境创建完全失败" -ForegroundColor Red
Write-Host "请手动检查Python安装" -ForegroundColor Yellow
return
}
}
# 6. 激活虚拟环境
Write-Host "6. 激活虚拟环境..." -ForegroundColor Yellow
& .\.venv\Scripts\Activate.ps1
# 7. 升级pip
Write-Host "7. 升级pip..." -ForegroundColor Yellow
python -m pip install --upgrade pip
# 8. 安装基础依赖(一个一个安装,避免冲突)
Write-Host "8. 安装依赖..." -ForegroundColor Yellow
$packages = @(
"PySide6==6.7.0",
"python-docx==0.8.11",
"pandas==2.2.2",
"matplotlib==3.8.4",
"influxdb-client==1.43.0",
"pymodbus==3.6.2",
"paramiko==3.4.0",
"psutil==5.9.8"
)
foreach ($pkg in $packages) {
Write-Host "安装 $pkg..." -ForegroundColor Cyan
pip install $pkg --no-cache-dir
if ($LASTEXITCODE -ne 0) {
Write-Host "$pkg 安装失败" -ForegroundColor Red
} else {
Write-Host "$pkg 安装成功" -ForegroundColor Green
}
}
# 9. 测试环境
Write-Host "9. 测试环境..." -ForegroundColor Yellow
python -c "
import sys
print(f'Python版本: {sys.version}')
try:
import PySide6
print('✅ PySide6导入成功')
except Exception as e:
print(f'❌ PySide6导入失败: {e}')
try:
from PySide6.QtCore import QTimer
print('✅ PySide6.QtCore导入成功')
except Exception as e:
print(f'❌ PySide6.QtCore导入失败: {e}')
"
Write-Host "强力清理完成!" -ForegroundColor Green
Write-Host "现在尝试运行: python main.py" -ForegroundColor Green