I have created (with AI help) two scripts one .bat (cmd - Visual Setup) and another .ps1 (PowerShell). With these scripts you can create a portable anaconda without superuser permissions. All the comments are in Spanish.
I have tested all and works smoothly. I only recomend use the link Anaconda Navigator to launch all the tools, but it creates links for everything.
run_install_anaconda_portable.bat
@echo off
setlocal enabledelayedexpansion
:: Directorio donde esta este script
set "SCRIPT_DIR=%~dp0"
:: Carpeta donde se instala Anaconda Portable
set "INSTALL_DIR=%SCRIPT_DIR%PortableAnaconda"
:: Ruta del script PowerShell
set "PS_SCRIPT=%SCRIPT_DIR%install_anaconda_portable.ps1"
echo.
echo ===============================
echo Instalacion portable de Anaconda
echo ===============================
echo.
:: Comprobacion basica existencia instalacion
if exist "%INSTALL_DIR%" (
set "INSTALLED=1"
) else (
set "INSTALLED=0"
)
:menu
echo Que deseas hacer?
echo.
echo 1. Instalar o Actualizar (descargar ultima version y actualizar)
echo 2. Reinstalar (usar el instalador ya descargado)
echo 3. Regenerar enlaces (crea los enlaces a partir de la instalacion)
echo 4. Desinstalar (borrar instalacion y enlaces)
echo 5. Salir
echo.
set /p "choice=Selecciona una opcion [1-4]: "
set "choice=!choice: =!"
if "!choice!"=="1" (
set "ACTION=Actualizar"
) else if "!choice!"=="2" (
set "ACTION=Reinstalar"
) else if "!choice!"=="3" (
set "ACTION=RegenerarEnlaces"
) else if "!choice!"=="4" (
set "ACTION=Desinstalar"
) else if "!choice!"=="5" (
echo Saliendo...
goto end
) else (
echo Opcion no valida.
goto menu
)
:: Detectar politica de ejecucion actual
for /f "tokens=*" %%p in ('powershell -Command "Get-ExecutionPolicy -Scope CurrentUser"') do set "CURRENT_POLICY=%%p"
echo Politica actual para CurrentUser: %CURRENT_POLICY%
if /i "%CURRENT_POLICY%" NEQ "RemoteSigned" (
echo Cambiando temporalmente politica de ejecucion a RemoteSigned para usuario actual...
powershell -Command "Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force"
)
echo.
powershell -ExecutionPolicy Bypass -NoProfile -Command "& { & '%PS_SCRIPT%' -Accion '%ACTION%' }"
:: Restaurar politica original si fue cambiada
if /i "%CURRENT_POLICY%" NEQ "RemoteSigned" (
echo.
echo Restaurando politica original de ejecucion...
powershell -Command "Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy %CURRENT_POLICY% -Force"
)
:end
echo.
pause
exit /b
install_anaconda_portable.ps1
param(
[Parameter(Mandatory = $true)]
[ValidateSet("Actualizar","Instalar","Reinstalar","RegenerarEnlaces","Desinstalar")]
[string]$Accion,
[string]$InstallDir = "$PSScriptRoot\PortableAnaconda"
)
function Get-LatestAnacondaUrl {
Write-Host "Obteniendo la última versión de Anaconda desde https://repo.anaconda.com/archive/ ..."
try {
$html = Invoke-WebRequest -Uri "https://repo.anaconda.com/archive/" -UseBasicParsing
$pattern = 'Anaconda3-\d{4}\.\d{2}(?:-\d+)?-Windows-x86_64\.exe'
$matches = [regex]::Matches($html.Content, $pattern) | ForEach-Object { $_.Value }
$latest = $matches | Sort-Object -Descending | Select-Object -First 1
if (-not $latest) {
Write-Error "No se pudo encontrar el nombre del instalador más reciente."
return $null
}
return "https://repo.anaconda.com/archive/$latest"
} catch {
Write-Error "Error al obtener la URL del instalador: $_"
return $null
}
}
function Download-Installer {
param (
[string]$Url,
[string]$Destination
)
if (Test-Path $Destination) {
Write-Host "El instalador ya existe: $Destination"
return
}
Write-Host "Descargando instalador desde $Url ..."
Invoke-WebRequest -Uri $Url -OutFile $Destination -UseBasicParsing
Write-Host "Descarga completada."
}
function Create-Shortcut {
param (
[string]$TargetPath,
[string]$ShortcutPath,
[string]$Arguments = "",
[string]$WorkingDirectory = "",
[string]$IconLocation = ""
)
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = $TargetPath
if ($Arguments) { $Shortcut.Arguments = $Arguments }
if ($WorkingDirectory) { $Shortcut.WorkingDirectory = $WorkingDirectory }
if ($IconLocation -and (Test-Path $IconLocation)) { $Shortcut.IconLocation = $IconLocation }
$Shortcut.Save()
}
function Create-Shortcuts {
param (
[string]$TargetDir
)
Write-Host "Creando accesos directos..."
$menuPath = Join-Path $TargetDir "Menu"
$targetCondaExe = Join-Path $TargetDir "Scripts\conda.exe"
# Python.exe
$lnkPython = Join-Path $PSScriptRoot "Anaconda-Python.lnk"
$targetPython = Join-Path $TargetDir "python.exe"
Create-Shortcut -TargetPath $targetPython -ShortcutPath $lnkPython -WorkingDirectory $TargetDir -IconLocation $targetPython
# Conda Prompt (CMD)
$lnkConda = Join-Path $PSScriptRoot "Anaconda-Condaprompt.lnk"
$argsConda = "shell.cmd.exe activate base & cmd.exe"
$iconConda = Join-Path $menuPath "anaconda_prompt.ico"
if (Test-Path $targetCondaExe) {
$finalArgs = "/k `"$targetCondaExe`" $argsConda"
Create-Shortcut -TargetPath "$env:WINDIR\System32\cmd.exe" -ShortcutPath $lnkConda -Arguments $finalArgs -WorkingDirectory $TargetDir -IconLocation $iconConda
}
# Conda Prompt (PowerShell)
$lnkPS = Join-Path $PSScriptRoot "Anaconda-Condaprompt-PowerShell.lnk"
$argsPS = "-NoExit -Command `"& `"$targetCondaExe`" shell.powershell activate base`""
$iconPS = Join-Path $menuPath "anaconda_powershell_prompt.ico"
if (Test-Path $targetCondaExe) {
Create-Shortcut -TargetPath "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" -ShortcutPath $lnkPS -Arguments $argsPS -WorkingDirectory $TargetDir -IconLocation $iconPS
}
# Anaconda Navigator (con entorno activado)
$lnkNavigator = Join-Path $PSScriptRoot "Anaconda-Navigator.lnk"
$iconNavigator = Join-Path $menuPath "anaconda-navigator.ico"
if (Test-Path $targetCondaExe) {
$argsNavigator = "/k `"$targetCondaExe`" run anaconda-navigator"
Create-Shortcut -TargetPath "$env:WINDIR\System32\cmd.exe" -ShortcutPath $lnkNavigator -Arguments $argsNavigator -WorkingDirectory $TargetDir -IconLocation $iconNavigator
}
# Jupyter Notebook
$lnkJupyter = Join-Path $PSScriptRoot "Jupyter-Notebook.lnk"
$iconJupyter = Join-Path $menuPath "jupyter.ico"
if (Test-Path $targetCondaExe) {
$argsJupyter = "/k `"$targetCondaExe`" run jupyter-notebook"
Create-Shortcut -TargetPath "$env:WINDIR\System32\cmd.exe" -ShortcutPath $lnkJupyter -Arguments $argsJupyter -WorkingDirectory $TargetDir -IconLocation $iconJupyter
}
# Spyder
$lnkSpyder = Join-Path $PSScriptRoot "Spyder.lnk"
$iconSpyder = Join-Path $menuPath "spyder.ico"
if (Test-Path $targetCondaExe) {
$argsSpyder = "/k `"$targetCondaExe`" run spyder"
Create-Shortcut -TargetPath "$env:WINDIR\System32\cmd.exe" -ShortcutPath $lnkSpyder -Arguments $argsSpyder -WorkingDirectory $TargetDir -IconLocation $iconSpyder
}
# QtConsole
$lnkQt = Join-Path $PSScriptRoot "QtConsole.lnk"
$iconQt = Join-Path $menuPath "qtconsole.ico"
if (Test-Path $targetCondaExe) {
$argsQt = "/k `"$targetCondaExe`" run jupyter-qtconsole"
Create-Shortcut -TargetPath "$env:WINDIR\System32\cmd.exe" -ShortcutPath $lnkQt -Arguments $argsQt -WorkingDirectory $TargetDir -IconLocation $iconQt
}
# Acceso directo en el escritorio al Anaconda Navigator (usando el .exe directamente)
$desktopShortcut = Join-Path "$env:USERPROFILE\Desktop" "Anaconda-Navigator.lnk"
$exeNavigator = Join-Path $TargetDir "Scripts\anaconda-navigator.exe"
if (Test-Path $exeNavigator) {
Create-Shortcut -TargetPath $exeNavigator `
-ShortcutPath $desktopShortcut `
-WorkingDirectory $TargetDir `
-IconLocation $iconNavigator
Write-Host "Acceso directo en escritorio creado: $desktopShortcut"
}
}
function Install-Anaconda {
param (
[string]$InstallerPath,
[string]$TargetDir
)
Write-Host "Instalando Anaconda..."
$args = "/InstallationType=JustMe /AddToPath=0 /RegisterPython=0 /S /D=$TargetDir"
Start-Process -FilePath $InstallerPath -ArgumentList $args -Wait -NoNewWindow
Write-Host "Instalación completada."
Create-Shortcuts -TargetDir $TargetDir
}
function Fast-DeleteFolder {
param([string]$Path)
if (-not (Test-Path $Path)) { return }
$null = robocopy "$env:TEMP" $Path /MIR /NJH /NJS /NP /R:0 /W:0
Remove-Item -LiteralPath $Path -Force -ErrorAction SilentlyContinue
}
function Verbose-DeleteFolder {
param (
[string]$Path
)
if (-not (Test-Path $Path)) {
Write-Host "La carpeta '$Path' no existe."
return
}
Write-Host "Archivos y carpetas a borrar..."
$items = Get-ChildItem -Path $Path -Recurse -Force -ErrorAction SilentlyContinue | Sort-Object FullName -Descending
foreach ($item in $items) {
try {
if ($item.PSIsContainer) {
Write-Host "Eliminando carpeta: $($item.FullName)"
Remove-Item -LiteralPath $item.FullName -Recurse -Force -ErrorAction SilentlyContinue
} else {
Write-Host "Eliminando archivo: $($item.FullName)"
Remove-Item -LiteralPath $item.FullName -Force -ErrorAction SilentlyContinue
}
} catch {
Write-Warning "No se pudo eliminar: $($item.FullName)"
}
}
# Finalmente, borra la carpeta raíz si sigue existiendo
try {
Write-Host "Eliminando carpeta raíz: $Path"
Remove-Item -LiteralPath $Path -Recurse -Force -ErrorAction SilentlyContinue
} catch {
Write-Warning "No se pudo eliminar la carpeta raíz: $Path"
}
Write-Host "Borrado completo."
}
function Uninstall-Anaconda {
param (
[string]$TargetDir
)
Write-Host "Iniciando desinstalación..."
if (-Not (Test-Path $TargetDir)) {
Write-Host "No existe la carpeta de instalación."
return
}
$confirm = Read-Host "¿Seguro que quieres desinstalar y borrar completamente '$TargetDir'? (S/N)"
if ($confirm -match '^[Ss]$') {
Write-Host "Borrando carpeta de instalación..."
Verbose-DeleteFolder -Path $TargetDir
# Elimina accesos directos dentro del directorio de instalación ($TargetDir), incluyendo subcarpetas.
#Get-ChildItem -Path $TargetDir -Filter *.lnk -Recurse -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
Write-Host "Borrando accesos directos junto al script..."
$shortcuts = @(
"Anaconda-Python.lnk",
"Anaconda-Condaprompt.lnk",
"Anaconda-Condaprompt-PowerShell.lnk",
"Anaconda-Navigator.lnk",
"Jupyter-Notebook.lnk",
"Spyder.lnk",
"QtConsole.lnk"
)
foreach ($lnk in $shortcuts) {
$lnkPath = Join-Path $PSScriptRoot $lnk
if (Test-Path $lnkPath) {
Remove-Item $lnkPath -Force -ErrorAction SilentlyContinue
Write-Host "Eliminado acceso directo: $lnk"
}
}
# Borrado del acceso directo en escritorio
$desktopShortcut = Join-Path "$env:USERPROFILE\Desktop" "Anaconda-Navigator.lnk"
if (Test-Path $desktopShortcut) {
Remove-Item $desktopShortcut -Force -ErrorAction SilentlyContinue
Write-Host "Eliminado acceso directo del escritorio: $desktopShortcut"
}
Write-Host "Desinstalación completada."
} else {
Write-Host "Desinstalación cancelada."
}
}
# Ejecutar acción
switch ($Accion) {
"Actualizar" {
$installerUrl = Get-LatestAnacondaUrl
if (-not $installerUrl) {
Write-Error "No se pudo obtener la URL del instalador."
break
}
$latestInstallerName = [System.IO.Path]::GetFileName($installerUrl)
$latestInstallerPath = Join-Path $PSScriptRoot $latestInstallerName
$localInstaller = Get-ChildItem -Path $PSScriptRoot -Filter "Anaconda*.exe" | Where-Object { $_.Name -eq $latestInstallerName }
if (-not $localInstaller) {
# Nueva versión disponible
Download-Installer -Url $installerUrl -Destination $latestInstallerPath
if (Test-Path $InstallDir) {
Uninstall-Anaconda -TargetDir $InstallDir
}
Install-Anaconda -InstallerPath $latestInstallerPath -TargetDir $InstallDir -ScriptDir $PSScriptRoot
}
else {
# No hay nueva versión
if (Test-Path $InstallDir) {
Write-Host "Ya tienes la última versión instalada. No se requiere actualización."
} else {
Write-Host "No hay nueva versión, pero no está instalado. Procediendo a instalar..."
Install-Anaconda -InstallerPath $latestInstallerPath -TargetDir $InstallDir -ScriptDir $PSScriptRoot
}
}
}
"Reinstalar" {
$installerFile = Get-ChildItem -Path $PSScriptRoot -Filter "Anaconda*.exe" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if (-not $installerFile) {
Write-Error "No se encontró instalador local en la carpeta."
break
}
if (Test-Path $InstallDir) {
Uninstall-Anaconda -TargetDir $InstallDir
}
Install-Anaconda -InstallerPath $installerFile.FullName -TargetDir $InstallDir -ScriptDir $PSScriptRoot
}
"RegenerarEnlaces" {
if (-not (Test-Path $InstallDir)) {
Write-Error "No existe la carpeta de instalación: $InstallDir"
break
}
Write-Host "Regenerando accesos directos..."
Create-Shortcuts -TargetDir $InstallDir
Write-Host "Accesos directos regenerados."
}
"Desinstalar" {
Uninstall-Anaconda -TargetDir $InstallDir
}
default {
Write-Error "Acción no reconocida: $Accion"
}
}