Created
November 30, 2024 03:33
-
-
Save 4piu/0a7e1c88edd2c31c1aa592a17e0dd3f9 to your computer and use it in GitHub Desktop.
Setup Python embeddable package as portable python. Installs `pip` and `virtualenv`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| param ( | |
| [Parameter(Mandatory = $true)] | |
| [string]$PythonFolderPath | |
| ) | |
| # Ensure the provided path exists | |
| if (-Not (Test-Path -Path $PythonFolderPath)) { | |
| Write-Error "The specified path '$PythonFolderPath' does not exist." | |
| exit 1 | |
| } | |
| # Get the python version from the embedded folder's pth file | |
| try { | |
| $pthFile = Get-ChildItem -Path $PythonFolderPath -Filter "python*._pth" | Select-Object -First 1 | |
| if (-Not $pthFile) { | |
| Write-Error "Could not find a valid python*._pth file in the folder '$PythonFolderPath'." | |
| exit 1 | |
| } | |
| $pthFilePath = $pthFile.FullName | |
| Write-Host "Found pth file: $pthFilePath" | |
| # Uncomment '#import site' in the pth file | |
| (Get-Content -Path $pthFilePath) | | |
| ForEach-Object { $_ -replace '^#import site', 'import site' } | | |
| Set-Content -Path $pthFilePath | |
| Write-Host "Uncommented '#import site' in the pth file." | |
| } catch { | |
| Write-Error "An error occurred while processing the pth file: $_" | |
| exit 1 | |
| } | |
| # Download and run get-pip.py | |
| $GetPipUrl = "https://bootstrap.pypa.io/get-pip.py" | |
| $GetPipScript = Join-Path -Path $PSScriptRoot -ChildPath "get-pip.py" | |
| try { | |
| Write-Host "Downloading get-pip.py from $GetPipUrl..." | |
| Invoke-WebRequest -Uri $GetPipUrl -OutFile $GetPipScript | |
| $PythonExe = Join-Path -Path $PythonFolderPath -ChildPath "python.exe" | |
| if (-Not (Test-Path -Path $PythonExe)) { | |
| Write-Error "Could not find python.exe in the folder '$PythonFolderPath'." | |
| exit 1 | |
| } | |
| Write-Host "Running get-pip.py..." | |
| & $PythonExe $GetPipScript | |
| Write-Host "Successfully installed pip." | |
| } catch { | |
| Write-Error "An error occurred while downloading or running get-pip.py: $_" | |
| exit 1 | |
| } | |
| # Install virtualenv using pip | |
| try { | |
| Write-Host "Installing virtualenv using pip..." | |
| & $PythonExe -m pip install virtualenv | |
| Write-Host "Successfully installed virtualenv." | |
| } catch { | |
| Write-Error "An error occurred while installing virtualenv: $_" | |
| exit 1 | |
| } | |
| Write-Host "Python Embedded setup completed successfully." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment