-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_python.ps1
More file actions
214 lines (182 loc) · 7.16 KB
/
fix_python.ps1
File metadata and controls
214 lines (182 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# OSI Visualizer - Script de Correção Python/pip para Windows
# Execute como Administrador
param(
[switch]$Force,
[string]$PythonVersion = "3.11"
)
Write-Host "🛠️ OSI Visualizer - Correção Python/pip" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
# Função para escrever com cores
function Write-Success { param($msg) Write-Host "✅ $msg" -ForegroundColor Green }
function Write-Error { param($msg) Write-Host "❌ $msg" -ForegroundColor Red }
function Write-Warning { param($msg) Write-Host "⚠️ $msg" -ForegroundColor Yellow }
function Write-Info { param($msg) Write-Host "ℹ️ $msg" -ForegroundColor Blue }
# Verificar se está executando como administrador
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if (-not $isAdmin) {
Write-Warning "Este script deve ser executado como Administrador"
Write-Info "Clique com botão direito no PowerShell e escolha 'Executar como Administrador'"
Read-Host "Pressione Enter para continuar mesmo assim ou Ctrl+C para sair"
}
Write-Info "Iniciando diagnóstico..."
# 1. Verificar instalações existentes do Python
Write-Info "1. Verificando instalações do Python..."
$pythonPaths = @()
$pythonCommands = @("python", "python3", "py")
foreach ($cmd in $pythonCommands) {
try {
$version = & $cmd --version 2>$null
if ($version) {
$path = & $cmd -c "import sys; print(sys.executable)" 2>$null
$pythonPaths += @{Command=$cmd; Version=$version; Path=$path}
Write-Success "$cmd encontrado: $version em $path"
}
}
catch {
Write-Warning "$cmd não encontrado"
}
}
# 2. Verificar pip
Write-Info "2. Verificando pip..."
$pipWorking = $false
foreach ($py in $pythonPaths) {
$cmd = $py.Command
try {
$pipVersion = & $cmd -m pip --version 2>$null
if ($pipVersion) {
Write-Success "pip funciona com $cmd`: $pipVersion"
$pipWorking = $true
$workingPython = $cmd
break
}
}
catch {
Write-Warning "pip não funciona com $cmd"
}
}
# 3. Se pip não funciona, tentar instalar
if (-not $pipWorking) {
Write-Warning "pip não está funcionando. Tentando corrigir..."
# Tentar baixar get-pip.py
Write-Info "Baixando get-pip.py..."
try {
$tempDir = $env:TEMP
$getPipPath = Join-Path $tempDir "get-pip.py"
Invoke-WebRequest -Uri "https://bootstrap.pypa.io/get-pip.py" -OutFile $getPipPath
Write-Success "get-pip.py baixado"
# Tentar instalar pip com cada versão do Python
foreach ($py in $pythonPaths) {
$cmd = $py.Command
Write-Info "Tentando instalar pip com $cmd..."
try {
& $cmd $getPipPath
$pipVersion = & $cmd -m pip --version 2>$null
if ($pipVersion) {
Write-Success "pip instalado com sucesso usando $cmd"
$pipWorking = $true
$workingPython = $cmd
break
}
}
catch {
Write-Warning "Falha ao instalar pip com $cmd"
}
}
# Limpar arquivo temporário
Remove-Item $getPipPath -ErrorAction SilentlyContinue
}
catch {
Write-Error "Erro ao baixar get-pip.py: $_"
}
}
# 4. Se ainda não funciona, sugerir instalação do Python
if (-not $pipWorking) {
Write-Error "Não foi possível corrigir o pip"
Write-Info "Soluções recomendadas:"
Write-Host " 1. Reinstalar Python de https://python.org/downloads/" -ForegroundColor Yellow
Write-Host " 2. Marcar 'Add Python to PATH' durante instalação" -ForegroundColor Yellow
Write-Host " 3. Marcar 'Install pip' durante instalação" -ForegroundColor Yellow
Write-Host " 4. Instalar Python da Microsoft Store" -ForegroundColor Yellow
# Oferecer para abrir o site
$openSite = Read-Host "Deseja abrir o site do Python? (y/n)"
if ($openSite -eq "y" -or $openSite -eq "Y") {
Start-Process "https://www.python.org/downloads/"
}
exit 1
}
# 5. Instalar dependências do projeto
Write-Info "3. Instalando dependências do OSI Visualizer..."
$backendPath = Join-Path (Get-Location) "backend"
$requirementsPath = Join-Path $backendPath "requirements.txt"
if (Test-Path $requirementsPath) {
Write-Info "Instalando dependências do backend..."
try {
Set-Location $backendPath
& $workingPython -m pip install -r requirements.txt
Write-Success "Dependências instaladas com sucesso"
}
catch {
Write-Error "Erro ao instalar dependências: $_"
}
finally {
Set-Location ..
}
}
else {
Write-Warning "requirements.txt não encontrado em $requirementsPath"
}
# 6. Verificar Node.js
Write-Info "4. Verificando Node.js..."
try {
$nodeVersion = node --version 2>$null
$npmVersion = npm --version 2>$null
if ($nodeVersion -and $npmVersion) {
Write-Success "Node.js: $nodeVersion"
Write-Success "npm: $npmVersion"
# Instalar dependências do frontend
$frontendPath = Join-Path (Get-Location) "frontend"
$packageJsonPath = Join-Path $frontendPath "package.json"
if (Test-Path $packageJsonPath) {
Write-Info "Instalando dependências do frontend..."
try {
Set-Location $frontendPath
npm install
Write-Success "Dependências do frontend instaladas"
}
catch {
Write-Error "Erro ao instalar dependências do frontend: $_"
}
finally {
Set-Location ..
}
}
}
else {
Write-Warning "Node.js não encontrado"
Write-Info "Baixe em: https://nodejs.org/"
}
}
catch {
Write-Warning "Node.js não encontrado"
Write-Info "Baixe em: https://nodejs.org/"
}
# 7. Teste final
Write-Info "5. Teste final..."
Write-Host "`n📊 RESUMO:" -ForegroundColor Cyan
Write-Host "=========" -ForegroundColor Cyan
if ($pipWorking) {
Write-Success "Python/pip funcionando: $workingPython"
Write-Host "`n🚀 Para executar o projeto:" -ForegroundColor Green
Write-Host "Backend (como Administrador):" -ForegroundColor Yellow
Write-Host " cd backend" -ForegroundColor White
Write-Host " $workingPython app.py" -ForegroundColor White
Write-Host "`nFrontend (outro terminal):" -ForegroundColor Yellow
Write-Host " cd frontend" -ForegroundColor White
Write-Host " npm run dev" -ForegroundColor White
Write-Host "`nAcesse: http://localhost:3000" -ForegroundColor Cyan
}
else {
Write-Error "Problemas não resolvidos. Consulte TROUBLESHOOTING.md"
}
Write-Host "`n✅ Script concluído!" -ForegroundColor Green
Read-Host "Pressione Enter para finalizar"