139 lines
5.5 KiB
PowerShell
139 lines
5.5 KiB
PowerShell
# Pharmacy Sales Prediction System - Portable Python Environment Setup
|
|
# This script will use the pre-downloaded Python 3.13.4 embedded package
|
|
|
|
# Set PowerShell to use UTF-8 encoding
|
|
$OutputEncoding = [System.Text.Encoding]::UTF8
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
|
|
|
|
# Set variables
|
|
$pythonVersion = "3.13.4"
|
|
$pythonZip = "python-$pythonVersion-embed-amd64.zip"
|
|
$pipUrl = "https://bootstrap.pypa.io/get-pip.py"
|
|
$envFolder = "portable_python"
|
|
$projectRoot = $PWD.Path
|
|
|
|
# Check if Python ZIP exists
|
|
$pythonZipPath = Join-Path -Path $envFolder -ChildPath $pythonZip
|
|
if (-not (Test-Path $pythonZipPath)) {
|
|
Write-Host "[!] Error: Python package not found at $pythonZipPath" -ForegroundColor Red
|
|
Write-Host " Please make sure the file exists in the specified location." -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Create environment directory if not exists
|
|
if (-not (Test-Path $envFolder)) {
|
|
Write-Host "[+] Creating portable Python environment directory..." -ForegroundColor Cyan
|
|
New-Item -ItemType Directory -Force -Path $envFolder | Out-Null
|
|
}
|
|
|
|
# Change to environment directory
|
|
Set-Location $envFolder
|
|
|
|
# Extract Python
|
|
Write-Host "[+] Extracting Python package..." -ForegroundColor Cyan
|
|
Expand-Archive -Path $pythonZip -DestinationPath . -Force
|
|
|
|
# Modify pth file to enable importing packages
|
|
$pthFile = Get-ChildItem -Filter "python*._pth" | Select-Object -First 1
|
|
if ($pthFile) {
|
|
Write-Host "[+] Configuring Python paths..." -ForegroundColor Cyan
|
|
$content = Get-Content $pthFile.FullName
|
|
$content = $content -replace "#import site", "import site"
|
|
$content | Set-Content $pthFile.FullName
|
|
}
|
|
|
|
# Download and install pip
|
|
Write-Host "[+] Downloading and installing pip..." -ForegroundColor Cyan
|
|
Invoke-WebRequest -Uri $pipUrl -OutFile "get-pip.py"
|
|
./python.exe get-pip.py
|
|
Remove-Item "get-pip.py"
|
|
|
|
# Create activation script
|
|
Write-Host "[+] Creating environment activation script..." -ForegroundColor Cyan
|
|
$activateContent = @"
|
|
# Activate portable Python environment
|
|
`$env:PYTHONHOME = "`$PSScriptRoot"
|
|
`$env:PATH = "`$PSScriptRoot;`$PSScriptRoot\Scripts;`$env:PATH"
|
|
Write-Host "[+] Portable Python environment activated!" -ForegroundColor Green
|
|
Write-Host "Use 'deactivate' command to exit the environment" -ForegroundColor Yellow
|
|
function global:deactivate {
|
|
# Restore original PATH and PYTHONHOME
|
|
`$env:PATH = `$env:PATH -replace [regex]::Escape("`$PSScriptRoot;`$PSScriptRoot\Scripts;"), ""
|
|
Remove-Item Env:PYTHONHOME -ErrorAction SilentlyContinue
|
|
Write-Host "[-] Exited portable Python environment" -ForegroundColor Magenta
|
|
# Remove the function itself
|
|
Remove-Item function:deactivate
|
|
}
|
|
"@
|
|
$activateContent | Set-Content "activate.ps1" -Encoding UTF8
|
|
|
|
# Install project dependencies
|
|
Write-Host "[+] Installing project dependencies..." -ForegroundColor Cyan
|
|
./python.exe -m pip install --upgrade pip setuptools wheel
|
|
./python.exe -m pip install -r "$projectRoot\portable_requirements.txt"
|
|
|
|
# Create script to run the project
|
|
Write-Host "[+] Creating project launcher script..." -ForegroundColor Cyan
|
|
$runScriptContent = @"
|
|
# Run Pharmacy Sales Prediction System
|
|
# Activate environment
|
|
. "`$PSScriptRoot\activate.ps1"
|
|
# Change to project directory
|
|
Set-Location "$projectRoot"
|
|
# Run main program with path configuration
|
|
python.exe -c "import sys; sys.path.insert(0, ''); import run_pharmacy_prediction"
|
|
"@
|
|
$runScriptContent | Set-Content "run_project.ps1" -Encoding UTF8
|
|
|
|
# Create script to run API service
|
|
$apiScriptContent = @"
|
|
# Run Pharmacy Sales Prediction API Service
|
|
# Activate environment
|
|
. "`$PSScriptRoot\activate.ps1"
|
|
# Change to project directory
|
|
Set-Location "$projectRoot"
|
|
# Run API service with path configuration
|
|
python.exe -c "import sys; sys.path.insert(0, ''); import api"
|
|
"@
|
|
$apiScriptContent | Set-Content "run_api.ps1" -Encoding UTF8
|
|
|
|
# Create a generic Python script launcher
|
|
$pythonRunnerContent = @"
|
|
# Generic Python Script Runner
|
|
# Activate environment
|
|
. "`$PSScriptRoot\activate.ps1"
|
|
# Change to project directory
|
|
Set-Location "$projectRoot"
|
|
|
|
# Check if script parameter was provided
|
|
if (`$args.Count -eq 0) {
|
|
Write-Host "[!] Error: No Python script specified" -ForegroundColor Red
|
|
Write-Host "Usage: .\run_script.ps1 <script_name.py>" -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
# Get script name from arguments
|
|
`$scriptName = `$args[0]
|
|
|
|
# Run Python script with path configuration
|
|
python.exe -c "import sys; sys.path.insert(0, ''); exec(open('`$scriptName').read())"
|
|
"@
|
|
$pythonRunnerContent | Set-Content "run_script.ps1" -Encoding UTF8
|
|
|
|
# Return to project root directory
|
|
Set-Location $projectRoot
|
|
|
|
Write-Host "`n[+] Portable Python environment setup completed!" -ForegroundColor Green
|
|
Write-Host "`nUsage Instructions:" -ForegroundColor Yellow
|
|
Write-Host "1. Enter environment: ./$envFolder/activate.ps1" -ForegroundColor White
|
|
Write-Host "2. Run project: ./$envFolder/run_project.ps1" -ForegroundColor White
|
|
Write-Host "3. Run API service: ./$envFolder/run_api.ps1" -ForegroundColor White
|
|
Write-Host "4. Run any script: ./$envFolder/run_script.ps1 <script_name.py>" -ForegroundColor White
|
|
Write-Host "5. Exit environment: deactivate" -ForegroundColor White
|
|
|
|
# GPU support information
|
|
Write-Host "`n[i] GPU Support Information:" -ForegroundColor Yellow
|
|
Write-Host "Default installation is PyTorch CPU version. To enable GPU support, run:" -ForegroundColor White
|
|
Write-Host "./$envFolder/activate.ps1" -ForegroundColor White
|
|
Write-Host "python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121" -ForegroundColor White |