2

How can I find out where Python was installed in a Windows 11 machine, so that I can use the address to add Python to the PATH variable?

The documentation I have found on this assumes that the user can already use the python command in the cli. But in this case, the cli cannot find python yet because python has not been added to the PATH yet.

Also, I looked closely in Windows file explorer and was not able to find Python in Program Files, Program Files (x86), the root of the C drive, or any of the many other places that I looked.

Here are the commands that first install python and then try to check the resulting Python version.

PS C:\Users\Administrator> C:\temp\python-3.11.4-amd64.exe /passive InstallAllUsers=0 InstallLauncherAllUsers=0 PrependPath=1 Include_test=0
PS C:\Users\Administrator> python --version
python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ python --version
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (python:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\Administrator>

The /passive flag above results in the python install GUI launching while the install happens, and then closing without giving any error message, so it seems clear that the install command is indeed running. ... If you can suggest any alterations to the command to elucidate any possible error message, I would be happy to try your suggested fully automated commands that might leave better logs.

This is on an Amazon EC2 instance being accessed remotely using an RDP file, if that makes any difference. This provisioning process must be completely automated.

Powershell is running as an administrator while invoking the above commands.

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • @Axe319 `PS C:\Users\Administrator> cd %LOCALAPPDATA%` results in `cd : Cannot find path 'C:\Users\Administrator\%LOCALAPPDATA%' because it does not exist.` – CodeMed Aug 03 '23 at 23:54
  • @Axe319 That works. Thank you. But to make it an actual answer, can you please show how to return the location of python in a variable whose value can be added to the PATH? $env:LOCALAPPDATA might not always resolve to the same value on different machines. – CodeMed Aug 04 '23 at 00:05
  • @Axe319 We are getting the location with `$ploc="$($env:LOCALAPPDATA)\Programs\Python\Python311\python.exe"` and then appending the value of `$ploc` to the PATH in a subsequent command. Will this be a reliable answer? If not, what syntax would you suggest instead? – CodeMed Aug 04 '23 at 00:30
  • 1
    @Axe319 If you want to write that up as an answer, I would be happy to mark it as accepted. – CodeMed Aug 04 '23 at 02:05

1 Answers1

1

The default install location for user installations on Windows uses the LOCALAPPDATA variable. This typically points towards C:\users\<username>\Appdata\Local\Programs\Python\Python<XY>\ where <XY> are the major and minor versions. In your case this would be C:\users\Administrator\Appdata\Local\Programs\Python\Python311\.

Source, specifically, here. You might note that there are PythonXY-32 and PythonXY-64 alternatives for 32 and 64 bit installations respectively, however, I believe these are no longer used in Windows 11 since the installer is now 64 bit by default. If you are using the same installer for all installs you should be able to rely on %LOCALAPPDATA%\Programs\Python\Python311\ as the path. (Or "$($env:LOCALAPPDATA)\Programs\Python\Python311\" in Powershell.

Python typically adds both "$($env:LOCALAPPDATA)\Programs\Python\Python311\" and "$($env:LOCALAPPDATA)\Programs\Python\Python311\Scripts\" to PATH since the Scripts directory will include helpful tools like pip, mypy, and pylint (if installed).

These could be run as subcommands (python -m mypy my_file.py) if Scripts isn't on the PATH, but most documentation will demonstrate them as being run directly (mypy my_file.py).

Axe319
  • 4,255
  • 3
  • 15
  • 31
  • Can you help with this [follow up question about how to do the install from a PowerShell script](https://stackoverflow.com/questions/76863325/install-python-from-powershell-script)? – CodeMed Aug 08 '23 at 22:10