如何找到Windows上Python的安装位置?

问题:如何找到Windows上Python的安装位置?

我想找出Windows上的Python安装路径。例如:

C:\Python25

如何找到Python的安装位置?

I want to find out my Python installation path on Windows. For example:

C:\Python25

How can I find where Python is installed?


回答 0

在您的Python解释器中,键入以下命令:

>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'

In your Python interpreter, type the following commands:

>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'

Also, you can club all these and use a single line command. Open cmd and enter following command

python -c "import os, sys; print(os.path.dirname(sys.executable))"

回答 1

如果您的环境变量中包含python,则可以在cmd中使用以下命令:

>>>其中python

或用于unix环境

>>>哪个python

命令行图片

If you have python in your enviroment variable then you can use the following command in cmd:

>>> where python

or for unix enviroment

>>> which python

command line image


回答 2

可能是

  • C:\ Python36
  • C:\ Users \(您登录的用户)\ AppData \ Local \ Programs \ Python \ Python36

It would be either of

  • C:\Python36
  • C:\Users\(Your logged in User)\AppData\Local\Programs\Python\Python36

回答 3

如果您需要了解Windows 的安装路径而不启动python解释器,请查看Windows注册表。

每个安装的Python版本在以下任一位置都有一个注册表项:

  • HKLM\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
  • HKCU\SOFTWARE\Python\PythonCore\versionnumber\InstallPath

在64位Windows中,它将位于Wow6432Node密钥下:

  • HKLM\SOFTWARE\Wow6432Node\Python\PythonCore\versionnumber\InstallPath

If you need to know the installed path under Windows without starting the python interpreter, have a look in the Windows registry.

Each installed Python version will have a registry key in either:

  • HKLM\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
  • HKCU\SOFTWARE\Python\PythonCore\versionnumber\InstallPath

In 64-bit Windows, it will be under the Wow6432Node key:

  • HKLM\SOFTWARE\Wow6432Node\Python\PythonCore\versionnumber\InstallPath

回答 4

在Windows安装上,我得到以下结果:

>>> import sys
>>> sys.executable
'C:\\Python26\\python.exe'
>>> sys.platform
'win32'
>>>

(您也可以寻找sys.path合理的位置。)

On my windows installation, I get these results:

>>> import sys
>>> sys.executable
'C:\\Python26\\python.exe'
>>> sys.platform
'win32'
>>>

(You can also look in sys.path for reasonable locations.)


回答 5

sys软件包中,您可以找到有关安装的许多有用信息:

import sys
print sys.executable
print sys.exec_prefix

我不确定这会在Windows系统上提供什么,但是在Mac上却executable指向Python二进制文件和exec_prefix安装根目录。

您也可以尝试使用以下方法检查sys模块:

import sys
for k,v in sys.__dict__.items():
    if not callable(v):
        print "%20s: %s" % (k,repr(v))

In the sys package, you can find a lot of useful information about your installation:

import sys
print sys.executable
print sys.exec_prefix

I’m not sure what this will give on your Windows system, but on my Mac executable points to the Python binary and exec_prefix to the installation root.

You could also try this for inspecting your sys module:

import sys
for k,v in sys.__dict__.items():
    if not callable(v):
        print "%20s: %s" % (k,repr(v))

回答 6

如果您想要安装成功后的路径,请先打开CMD并输入python或python -i

它将为您打开交互式外壳,然后键入

导入系统

可执行文件

点击Enter键,您将获得安装Python的路径…

If You want the Path After successful installation then first open you CMD and type python or python -i

It Will Open interactive shell for You and Then type

import sys

sys.executable

Hit enter and you will get path where your python is installed …


回答 7

简单的方法是

1) open CMD
2) type >>where python

Simple way is

1) open CMD
2) type >>where python

回答 8

您可以搜索“您帐户的环境变量”。如果您在路径中添加了Python,那么它将在您的环境变量帐户中显示为“路径”。

但几乎总是可以在“ C:\ Users \%User_name%\ AppData \ Local \ Programs \ Python \ Python_version ”中找到它

AppData ”文件夹可能已隐藏,从工具栏的“ 视图”部分可见。

You can search for the “environmental variable for you account”. If you have added the Python in the path, it’ll show as “path” in your environmental variable account.

but almost always you will find it in “C:\Users\%User_name%\AppData\Local\Programs\Python\Python_version

the ‘AppData‘ folder may be hidden, make it visible from the view section of toolbar.


回答 9

要知道Python的安装位置,可以where python在cmd.exe中执行。

To know where Python is installed you can execute where python in your cmd.exe.


回答 10

如果有人需要在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");
}

If anyone needs to do this in C# I’m using the following code:

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");
}

回答 11

如果不存在,请转到C:\ Users \ USER \ AppData \ Local \ Programs \ Python \ Python36,然后通过Windows + ^ R打开控制台,然后键入cmd并按Enter键键入python(如果安装在本地文件中)将显示其版本从那里输入以下import os import sys os.path.dirname(sys.executable)

Go to C:\Users\USER\AppData\Local\Programs\Python\Python36 if it is not there then open console by windows+^R Then type cmd and hit enter type python if installed in your local file it will show you its version from there type the following import os import sys os.path.dirname(sys.executable)


回答 12

如果您已经py安装了命令(可能已安装),则只需使用--list-paths命令的参数即可:

py --list-paths

输出示例:

py Launcher对于Windows
-3.8-32 C:\ Users \ cscott \ AppData \ Local \ Programs \ Python \ Python38-32 \ python.exe *
-2.7-64 C:\ Python27 \ python.exe 找到的已安装Python

*表示使用该py命令执行的脚本的当前活动版本。

If you have the py command installed, which you likely do, then just use the --list-paths argument to the command:

py --list-paths

Example output:

Installed Pythons found by py Launcher for Windows
-3.8-32 C:\Users\cscott\AppData\Local\Programs\Python\Python38-32\python.exe *
-2.7-64 C:\Python27\python.exe

The * indicates the currently active version for scripts executed using the py command.


回答 13

这对我有用: C:\Users\Your_user_name\AppData\Local\Programs\Python

我当前安装的python version3.7.0

希望这可以帮助!

This worked for me: C:\Users\Your_user_name\AppData\Local\Programs\Python

My currently installed python version is 3.7.0

Hope this helps!


回答 14

一般而言

‘C:\ Users \用户名\ AppData \ Local \ Programs \ Python \ Python-version’

或尝试使用(在cmd中)

哪里python

Its generally

‘C:\Users\user-name\AppData\Local\Programs\Python\Python-version’

or try using (in cmd )

where python


回答 15

如果您仍然卡住或得到这个

C:\\\Users\\\name of your\\\AppData\\\Local\\\Programs\\\Python\\\Python36

只需将2替换为1

C:\Users\akshay\AppData\Local\Programs\Python\Python36

if you still stuck or you get this

C:\\\Users\\\name of your\\\AppData\\\Local\\\Programs\\\Python\\\Python36

simply do this replace 2 \ with one

C:\Users\akshay\AppData\Local\Programs\Python\Python36

回答 16

我安装了2和3,发现3时遇到了相同的问题。幸运的是,在Windows路径中键入path可以让我找到安装它的位置。安装刚忘记的Python时,路径是一个选项。如果在安装Python 3时未选择设置路径,则可能无法使用-除非在安装时手动更新了路径。就我而言,它位于c:\ Program Files \ Python37 \ python.exe

I installed 2 and 3 and had the same problem finding 3. Fortunately, typing path at the windows path let me find where I had installed it. The path was an option when I installed Python which I just forgot. If you didn’t select setting the path when you installed Python 3 that probably won’t work – unless you manually updated the path when you installed it. In my case it was at c:\Program Files\Python37\python.exe


回答 17

如果anaconda navigator在Windows上使用,也可以enviornments在环境上滚动,该root环境将指示其安装位置。当您需要将此环境连接到要在其中集成一些python代码的其他应用程序时,可以使用该环境。

If you use anaconda navigator on windows, you can go too enviornments and scroll over the enviornments, the root enviorment will indicate where it is installed. It can help if you want to use this enviorment when you need to connect this to other applications, where you want to integrate some python code.