我想知道我在Windows上的Python安装路径。例如:
C:\Python25
如何找到Python的安装位置?
我想知道我在Windows上的Python安装路径。例如:
C:\Python25
如何找到Python的安装位置?
当前回答
您的机器上可能安装了多个版本的Python。所以如果你在Windows的命令提示符下,输入这样的东西…
py --version
...应该会告诉你现在用的是哪个版本。(如果py不起作用,可能会用python或python3替换py)。总之你会看到
Python 3.10.2
如果你用这样的东西创建一个虚拟环境…
py -m venv venv
...该环境也将使用该Python版本。要验证,激活环境…
venv\scripts\activate.bat
您将看到命令提示符的名称。现在如果执行:
where python
...它将显示虚拟环境使用的Python可执行文件。它将是Python.exe的副本,实际上在虚拟环境文件夹的Scripts子文件夹中。当然,要查看是哪个版本,再次使用py——version。
其他回答
选项1:检查系统环境变量>路径
选项2:C:\Users\Asus\AppData\Local\Programs\Python(默认路径)
在我的windows安装,我得到这些结果:
>>> import sys
>>> sys.executable
'C:\\Python26\\python.exe'
>>> sys.platform
'win32'
>>>
(你也可以在sys。合理位置的路径)
如果有人需要在c#中这样做,我使用以下代码:
static string GetPythonExecutablePath(int major = 3)
{
var software = "SOFTWARE";
var key = Registry.CurrentUser.OpenSubKey(software);
if (key == null)
key = Registry.LocalMachine.OpenSubKey(software);
if (key == null)
return null;
var pythonCoreKey = key.OpenSubKey(@"Python\PythonCore");
if (pythonCoreKey == null)
pythonCoreKey = key.OpenSubKey(@"Wow6432Node\Python\PythonCore");
if (pythonCoreKey == null)
return null;
var pythonVersionRegex = new Regex("^" + major + @"\.(\d+)-(\d+)$");
var targetVersion = pythonCoreKey.GetSubKeyNames().
Select(n => pythonVersionRegex.Match(n)).
Where(m => m.Success).
OrderByDescending(m => int.Parse(m.Groups[1].Value)).
ThenByDescending(m => int.Parse(m.Groups[2].Value)).
Select(m => m.Groups[0].Value).First();
var installPathKey = pythonCoreKey.OpenSubKey(targetVersion + @"\InstallPath");
if (installPathKey == null)
return null;
return (string)installPathKey.GetValue("ExecutablePath");
}
你可以在Windows GUI中找到它,但你需要在菜单中选择“显示隐藏”。在我的Win10电脑上安装python的目录:
C:\Users\username\AppData\Local\Programs\Python\Python310
如果你使用python pip来安装包,这非常方便。
如果你的环境变量中有Python,那么你可以在cmd或powershell中使用以下命令:
where python
或Unix环境
which python
命令行映像: