149 lines
4.9 KiB
PowerShell
149 lines
4.9 KiB
PowerShell
# MOTM Flask Application - Virtual Environment Setup (PowerShell)
|
|
# Run this script in PowerShell: .\setup_venv.ps1
|
|
|
|
Write-Host "MOTM Flask Application - Virtual Environment Setup" -ForegroundColor Green
|
|
Write-Host "=" * 60 -ForegroundColor Green
|
|
|
|
# Check Python installation
|
|
Write-Host "`n🐍 Checking Python installation..." -ForegroundColor Yellow
|
|
|
|
$pythonCommands = @('python', 'python3', 'py')
|
|
$pythonCmd = $null
|
|
|
|
foreach ($cmd in $pythonCommands) {
|
|
try {
|
|
$version = & $cmd --version 2>$null
|
|
if ($LASTEXITCODE -eq 0) {
|
|
$pythonCmd = $cmd
|
|
Write-Host "✅ Found Python: $version" -ForegroundColor Green
|
|
break
|
|
}
|
|
}
|
|
catch {
|
|
# Continue to next command
|
|
}
|
|
}
|
|
|
|
if (-not $pythonCmd) {
|
|
Write-Host "❌ Python not found. Please install Python 3.7+ from python.org" -ForegroundColor Red
|
|
Write-Host " Make sure to check 'Add Python to PATH' during installation" -ForegroundColor Yellow
|
|
Read-Host "Press Enter to exit"
|
|
exit 1
|
|
}
|
|
|
|
# Check if virtual environment already exists
|
|
$venvDir = "venv"
|
|
if (Test-Path $venvDir) {
|
|
Write-Host "`n⚠ Virtual environment '$venvDir' already exists" -ForegroundColor Yellow
|
|
$recreate = Read-Host "Do you want to recreate it? (y/N)"
|
|
if ($recreate -match '^[yY]') {
|
|
Write-Host "🗑️ Removing existing virtual environment..." -ForegroundColor Yellow
|
|
Remove-Item -Recurse -Force $venvDir
|
|
} else {
|
|
Write-Host "Using existing virtual environment..." -ForegroundColor Green
|
|
$skipCreation = $true
|
|
}
|
|
}
|
|
|
|
# Create virtual environment
|
|
if (-not $skipCreation) {
|
|
Write-Host "`n📦 Creating virtual environment in '$venvDir'..." -ForegroundColor Yellow
|
|
try {
|
|
& $pythonCmd -m venv $venvDir
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host "✅ Virtual environment created successfully!" -ForegroundColor Green
|
|
} else {
|
|
throw "Virtual environment creation failed"
|
|
}
|
|
}
|
|
catch {
|
|
Write-Host "❌ Failed to create virtual environment: $_" -ForegroundColor Red
|
|
Read-Host "Press Enter to exit"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Install dependencies
|
|
Write-Host "`n📚 Installing dependencies..." -ForegroundColor Yellow
|
|
|
|
$pipCmd = Join-Path $venvDir "Scripts\pip.exe"
|
|
if (-not (Test-Path $pipCmd)) {
|
|
$pipCmd = Join-Path $venvDir "Scripts\pip"
|
|
}
|
|
|
|
try {
|
|
# Upgrade pip first
|
|
Write-Host "🔄 Upgrading pip..." -ForegroundColor Yellow
|
|
& $pipCmd install --upgrade pip
|
|
|
|
# Install requirements
|
|
Write-Host "📦 Installing application dependencies..." -ForegroundColor Yellow
|
|
& $pipCmd install -r requirements.txt
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host "✅ Dependencies installed successfully!" -ForegroundColor Green
|
|
} else {
|
|
throw "Dependencies installation failed"
|
|
}
|
|
}
|
|
catch {
|
|
Write-Host "❌ Failed to install dependencies: $_" -ForegroundColor Red
|
|
Read-Host "Press Enter to exit"
|
|
exit 1
|
|
}
|
|
|
|
# Create convenience scripts
|
|
Write-Host "`n📝 Creating convenience scripts..." -ForegroundColor Yellow
|
|
|
|
# Update the batch files to use the correct Python command
|
|
$activateScript = @"
|
|
@echo off
|
|
echo 🐍 Activating MOTM Virtual Environment...
|
|
call venv\Scripts\activate.bat
|
|
echo ✅ Virtual environment activated!
|
|
echo.
|
|
echo 🚀 To start the MOTM application, run:
|
|
echo $pythonCmd main.py
|
|
echo.
|
|
echo 🔧 To deactivate, run:
|
|
echo deactivate
|
|
"@
|
|
|
|
$runScript = @"
|
|
@echo off
|
|
echo 🐍 Starting MOTM Application...
|
|
call venv\Scripts\activate.bat
|
|
$pythonCmd main.py
|
|
pause
|
|
"@
|
|
|
|
$activateScript | Out-File -FilePath "activate_motm.bat" -Encoding ASCII
|
|
$runScript | Out-File -FilePath "run_motm.bat" -Encoding ASCII
|
|
|
|
Write-Host "✅ Convenience scripts created!" -ForegroundColor Green
|
|
|
|
# Print completion message
|
|
Write-Host "`n" + "=" * 60 -ForegroundColor Green
|
|
Write-Host "🎉 MOTM Virtual Environment Setup Complete!" -ForegroundColor Green
|
|
Write-Host "=" * 60 -ForegroundColor Green
|
|
|
|
Write-Host "`n📋 To use the virtual environment:" -ForegroundColor Cyan
|
|
Write-Host " 1. Activate: .\activate_motm.bat" -ForegroundColor White
|
|
Write-Host " 2. Run app: .\run_motm.bat" -ForegroundColor White
|
|
Write-Host " 3. Deactivate: deactivate" -ForegroundColor White
|
|
|
|
Write-Host "`n🔧 Manual activation:" -ForegroundColor Cyan
|
|
Write-Host " venv\Scripts\activate.bat" -ForegroundColor White
|
|
Write-Host " $pythonCmd main.py" -ForegroundColor White
|
|
|
|
Write-Host "`n🌐 The application will be available at: http://localhost:5000" -ForegroundColor Cyan
|
|
|
|
Write-Host "`n📚 For development:" -ForegroundColor Cyan
|
|
Write-Host " - Activate the venv before installing new packages" -ForegroundColor White
|
|
Write-Host " - Use 'pip install <package>' to add dependencies" -ForegroundColor White
|
|
Write-Host " - Update requirements.txt with 'pip freeze > requirements.txt'" -ForegroundColor White
|
|
|
|
Write-Host "`n🚀 Ready to start! Run: .\run_motm.bat" -ForegroundColor Green
|
|
Read-Host "`nPress Enter to exit"
|
|
|