Python安装在哪里?

huangapple go评论83阅读模式
英文:

Where was python installed?

问题

如何找出Python安装在Windows 11机器上的位置,以便将Python添加到PATH变量中?

我找到的文档假定用户已经可以在命令行中使用python命令。但在这种情况下,命令行还找不到python,因为python尚未添加到PATH中。

此外,我在Windows文件资源管理器中仔细查看,并未能在Program Files、Program Files (x86)、C驱动器的根目录或其他许多地方找到Python。

以下是首先安装python然后尝试检查结果Python版本的命令。

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 : 无法将术语 'python' 识别为 cmdlet、函数、脚本文件或可操作的程序的名称。
请检查名称的拼写,或者如果包含了路径,请验证路径是否正确,然后重试。
在行:1 字符:1
+ python --version
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (python:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\Administrator>

上述的/passive标志导致python安装GUI启动,然后在安装完成后关闭,而不提供任何错误消息,因此似乎安装命令确实在运行。如果您可以建议对命令进行任何修改以阐明可能的错误消息,我将很乐意尝试您建议的完全自动化的命令,可能会生成更好的日志。

这是在通过RDP文件远程访问的Amazon EC2实例上进行的,如果有任何不同之处,这个配置过程必须是完全自动化的。

在调用上述命令时,PowerShell以管理员权限运行。

英文:

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.

答案1

得分: 1

默认情况下,Windows 用户安装的默认安装位置使用 LOCALAPPDATA 变量。这通常指向 C:\users\<username>\Appdata\Local\Programs\Python\Python<XY>\,其中 <XY> 是主要和次要版本号。在您的情况下,这将是 C:\users\Administrator\Appdata\Local\Programs\Python\Python311\

来源,具体来说,在这里。您可能会注意到,对于32位和64位安装,有 PythonXY-32PythonXY-64 的替代方案,然而,我 相信 自 Windows 11 起,安装程序默认为64位,因此不再使用这些替代方案。如果您在所有安装中都使用相同的安装程序,您应该能够依赖于 %LOCALAPPDATA%\Programs\Python\Python311\ 作为路径。(或在Powershell中使用 "$($env:LOCALAPPDATA)\Programs\Python\Python311\")。

Python通常将 "$($env:LOCALAPPDATA)\Programs\Python\Python311\""$($env:LOCALAPPDATA)\Programs\Python\Python311\Scripts\" 都添加到 PATH,因为 Scripts 目录将包括有用的工具,如 pipmypypylint(如果已安装)。

如果 Scripts 不在 PATH 上,这些工具 可以 作为子命令运行(python -m mypy my_file.py),但大多数文档将演示它们直接运行(mypy my_file.py)。

英文:

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).

huangapple
  • 本文由 发表于 2023年8月4日 07:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定