Skip to content

Instantly share code, notes, and snippets.

@matham
Created February 11, 2016 01:34
Show Gist options
  • Save matham/7c03ad437542b63e90a8 to your computer and use it in GitHub Desktop.
Save matham/7c03ad437542b63e90a8 to your computer and use it in GitHub Desktop.
@setlocal enableextensions enabledelayedexpansion
@echo off
REM Script that tries to find python in the registry and
REM adds it to the path. It uses the filename to find the
REM version of python to use. If `x64` or `_64` is in the
REM filename, then the 64 bit version of python is used.
REM Also, if e.g. 27 is in the filename, it loads python
REM 2.7. Similarly for other python versions.
REM NOTE: When installing as user rather than global,
REM python < 3.5 saves both 64 bit and 32 bit to the same
REM registry location, so they cannot be discovered side
REM by side, and the last install will be used.
set name=%~n0
set is86=1
if not x%name:x64=%==x%name% set is86=0
if not x%name:_64=%==x%name% set is86=0
if not x%name:27=%==x%name% set pyver=2.7
if not x%name:31=%==x%name% set pyver=3.1
if not x%name:32=%==x%name% set pyver=3.2
if not x%name:33=%==x%name% set pyver=3.3
if not x%name:34=%==x%name% set pyver=3.4
if not x%name:35=%==x%name% set pyver=3.5
if not x%name:36=%==x%name% set pyver=3.6
if not x%name:37=%==x%name% set pyver=3.7
if not x%name:38=%==x%name% set pyver=3.8
if "%is86%"=="1" (
set key=HKEY_LOCAL_MACHINE\Software\Wow6432Node\Python\PythonCore\
set suffix=-32
) else (
set key=HKEY_LOCAL_MACHINE\Software\Python\PythonCore\
set suffix=-64
)
if not exist "%PROGRAMFILES(X86)%" (
set key=HKEY_LOCAL_MACHINE\Software\Python\PythonCore\
)
set alt_key=HKEY_CURRENT_USER\Software\Python\PythonCore\
reg query %alt_key%%pyver% > nul 2>&1
if !errorlevel! equ 0 (
set root=%alt_key%%pyver%
) else (
reg query %alt_key%%pyver%%suffix% > nul 2>&1
if !errorlevel! equ 0 (
set root=%alt_key%%pyver%%suffix%
) else (
reg query %key%%pyver% > nul 2>&1
if !errorlevel! equ 0 (
set root=%key%%pyver%
) else (
set root=%key%%pyver%%suffix%
)
)
)
set root=%root%\InstallPath
reg query !root! > nul 2>&1
set val=""
set def=(Default)
if !errorlevel! equ 0 (
FOR /F "usebackq tokens=1-3" %%A IN (`reg query !root!`) DO (
if %%A==!def! set val=%%C
)
if not !val:~-1!==\ set val=!val!\
set PATH=!val!;!val!Scripts;!PATH!
)
python -c ""
IF !errorlevel! NEQ 0 (
echo Cannot find valid python in the registry
pause
goto END
)
echo Successfully found Python
cmd
:END
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment