Fix null reference error in first-time installation: move $pythonPath definition before service creation
## Bug Fix
### Problem
Backend deployment failed with error:
```
[INSTALLATION FAILED] You cannot call a method on a null-valued expression.
at Install-BackendFirstTime, line 609
```
The error occurred because `$pythonPath` was being used in service creation (line 609), but it was defined inside the `if (Test-Path $requirementsPath)` block. If requirements.txt didn't exist (or for any reason the block was skipped), the variable would be null when used later.
### Root Cause
Variable scope issue - paths were defined inside the requirements check block:
```powershell
# Before (WRONG):
if (Test-Path $requirementsPath) {
$pipPath = Join-Path $venvPath "Scripts\pip.exe"
$pythonPath = Join-Path $venvPath "Scripts\python.exe" # Only defined if requirements exists!
...
}
# Later:
& nssm install ... $pythonPath ... # ERROR: $pythonPath is null!
```
### Solution
Moved path definitions BEFORE the if block to ensure they're always available:
```powershell
# After (CORRECT):
$pipPath = Join-Path $venvPath "Scripts\pip.exe"
$pythonPath = Join-Path $venvPath "Scripts\python.exe" # Always defined
$requirementsPath = Join-Path $Config.BackendPath "requirements.txt"
if (Test-Path $requirementsPath) {
# Use the paths here
}
# Later:
& nssm install ... $pythonPath ... # OK: $pythonPath is defined
```
### Changes
- `Install-BackendFirstTime`: Moved `$pipPath`, `$pythonPath`, `$requirementsPath` definitions outside the if block
- `Install-TelegramBotFirstTime`: Applied the same fix to prevent future issues
### Tested
This ensures paths are always available for service creation, regardless of whether requirements.txt exists or the if block executes.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -570,12 +570,14 @@ function Install-BackendFirstTime {
|
||||
& python -m venv $venvPath
|
||||
Write-Success "Virtual environment created at: $venvPath"
|
||||
|
||||
# Install dependencies
|
||||
Write-Step "Installing Python dependencies..."
|
||||
# Define paths (BEFORE installing dependencies)
|
||||
$pipPath = Join-Path $venvPath "Scripts\pip.exe"
|
||||
$pythonPath = Join-Path $venvPath "Scripts\python.exe"
|
||||
$requirementsPath = Join-Path $Config.BackendPath "requirements.txt"
|
||||
|
||||
# Install dependencies
|
||||
Write-Step "Installing Python dependencies..."
|
||||
|
||||
if (Test-Path $requirementsPath) {
|
||||
Write-Info "Upgrading pip..."
|
||||
& $pythonPath -m pip install --upgrade pip | Out-Default
|
||||
@@ -768,12 +770,14 @@ function Install-TelegramBotFirstTime {
|
||||
& python -m venv $venvPath
|
||||
Write-Success "Virtual environment created"
|
||||
|
||||
# Install dependencies
|
||||
Write-Step "Installing Python dependencies..."
|
||||
# Define paths (BEFORE installing dependencies)
|
||||
$pipPath = Join-Path $venvPath "Scripts\pip.exe"
|
||||
$pythonPath = Join-Path $venvPath "Scripts\python.exe"
|
||||
$requirementsPath = Join-Path $Config.TelegramBotPath "requirements.txt"
|
||||
|
||||
# Install dependencies
|
||||
Write-Step "Installing Python dependencies..."
|
||||
|
||||
if (Test-Path $requirementsPath) {
|
||||
Write-Info "Upgrading pip..."
|
||||
& $pythonPath -m pip install --upgrade pip | Out-Default
|
||||
|
||||
Reference in New Issue
Block a user