9

When installing python 2.7 on Windows using silent installer (.msi), is there a command-line option to add Python to path environment variable, like the GUI option?

Python 3.5 installer includes an option PrependPath=0 by default, but can Python 2.7 use it?

https://docs.python.org/3/using/windows.html

Looks like this issue was discussed here, but no resolution for Python 2.7?

https://bugs.python.org/issue3561

EDIT


this batch command rocks!!!

setx \M PATH "%PATH%;C:\Python\Python27;C:\Python\Python27\Scripts"

but setx will truncate the stored %PATH% string to 1024 bytes.

denfromufa
  • 4,995
  • 11
  • 66
  • 130

2 Answers2

13

The Python MSI installer can update the system path since 2.4. Just add ADDLOCAL=ALL to the command line. You'll have to restart your system before it propagates.

msiexec /i "python-2.7.11.amd64.msi" /passive /norestart ADDLOCAL=ALL

https://www.python.org/download/releases/2.4/msi/

tahoar
  • 1,670
  • 3
  • 20
  • 35
  • "You'll have to restart your system before it propagates." - this is not an option in my case. – denfromufa Aug 24 '16 at 13:50
  • 4
    Perhaps the author meant "restart prompt" instead of system. Running msiexec as above with ADDLOCAL=ALL successfully introduces python to PATH in your next cmd session. – Chris Oct 11 '16 at 18:36
  • Yes, Chris, I meant system restart. This option updates the registry but it does not force Windows Explorer/Shell to reload the new path value. There's a way to do that, but apparently the Python installer doesn't use it. So, if you launch a program from Explorer after running the installation, the new process inherits the old path, not the registry path. I haven't tested this in ~8 months. Maybe it's changed. – tahoar Oct 13 '16 at 03:15
  • 1
    I just tested this on Windows XP (I know) and can confirm an entire restart was required after using `ADDLOCAL=ALL` with the Python 3.4 MSI installer. – jjj Mar 23 '17 at 18:33
  • Is restart needed also for 3.5.exe Installer? If i start Installer manually there is no need of restart. :/ I am dealing with this problem here: https://stackoverflow.com/questions/47310884/adding-into-path-var-while-silent-installation-of-python-possible-bug – Erik Šťastný Nov 16 '17 at 09:06
  • I've been using this command line successfully for 2.7.14 and %PATH% updates seem to propagate msiexec /norestart /passive /log ".\python-2.7.14.amd64.log" /package ".\python-2.7.14.amd64.msi" ADDLOCAL=ALL – tahoar Nov 22 '17 at 05:01
3

I have observed that on Windows 7 (Professional) with python 2.7.14 x64, no restart is required for Python to be added to PATH. Just start up a new command window after the install and python will be in the PATH.

You can determine whether or not a restart is required by the install by running the msi as follows:

start/wait "" msiexec /i "python-2.7.11.amd64.msi" /passive /norestart ADDLOCAL=ALL
if %errorlevel% == 3010 ( echo Success: reboot required ) else (if %errorlevel% == 0 ( echo Success ) else ( echo Installation failed with error code %errorlevel% ) )

That is, if %errorlevel% is 3010 (ERROR_SUCCESS_REBOOT_REQUIRED), then a reboot will be required. The use of start/wait causes cmd.exe to wait until the msiexec process finishes. This allows the msiexec return status to be available to cmd.exe.

BTW You may wish to include the option ALLUSERS=1 on the command line if you want the installation of Python to be available to all users on the system.

J. Beattie
  • 156
  • 1
  • 6