6

我有.py与 Python 解释器关联的扩展名的文件。但是,当我在命令行中键入此类文件的名称并按下时ENTER,该文件将在默认文本编辑器 (Notepad++) 中打开,而不是使用相关的 Python 解释器运行。为什么?

Z:\1>where python
c:\Program Files\Python\2.7\python.exe

Z:\1>reg query HKCR\.py /ve

HKEY_CLASSES_ROOT\.py
    (Default)    REG_SZ    Python.File


Z:\1>reg query HKCR\Python.File\shell\open\command /ve

HKEY_CLASSES_ROOT\Python.File\shell\open\command
    (Default)    REG_SZ    c:\Program Files\Python\2.7\python.exe "%1" %*


Z:\1>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW

Z:\1>type a.py
from __future__ import print_function; import sys; print(sys.executable); print(sys.version)
Z:\1>a.py    <--- this opens a.py in the default text editor

我在 Windows Vista x64 上使用Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32

更新

删除HKEY_CLASSES_ROOT\Unknown\shell\notepad++\command密钥后(我过去在将无扩展名的文件与 Notepad++ 关联的过程中创建了此密钥 - 请参阅如何设置默认程序以在 Windows 中打开无扩展名的文件?),其值为C:\Program Files (x86)\Notepad++\notepad++ %1我收到此错误:

此文件没有与之关联的用于执行此操作的程序。在“设置关联”控制面板中创建关联。

更新 2

要显示系统正常使用的所有注册表项以找出与.py扩展相关联的程序,我运行以下批处理文件 ( python_assoc.query.bat):

@echo off
if "%~1" == "details" (
    set keypath=HKLM\Software\Classes
    echo ----- HKEY_LOCAL_MACHINE -----
    call :query
    set keypath=HKCU\Software\Classes
    echo.
    echo ----- HKEY_CURRENT_USER -----
    call :query
) else (
    set keypath=HKCR
    echo ----- HKEY_CLASSES_ROOT -----
    call :query
)   
goto end

:query
reg query %keypath%\.py /ve
reg query %keypath%\.pyw /ve
reg query %keypath%\.pyc /ve
reg query %keypath%\.pyo /ve

reg query %keypath%\Python.File\shell\open\command /ve
reg query %keypath%\Python.NoConFile\shell\open\command /ve
reg query %keypath%\Python.CompiledFile\shell\open\command /ve
goto :eof

:end
set keypath=

输出

Z:\1>python_assoc.query.bat details
----- HKEY_LOCAL_MACHINE -----

HKEY_LOCAL_MACHINE\Software\Classes\.py
    (Default)    REG_SZ    Python.File
HKEY_LOCAL_MACHINE\Software\Classes\.pyw
    (Default)    REG_SZ    Python.NoConFile
HKEY_LOCAL_MACHINE\Software\Classes\.pyc
    (Default)    REG_SZ    Python.CompiledFile
HKEY_LOCAL_MACHINE\Software\Classes\.pyo
    (Default)    REG_SZ    Python.CompiledFile
HKEY_LOCAL_MACHINE\Software\Classes\Python.File\shell\open\command
    (Default)    REG_SZ    c:\Program Files\Python\2.7\python.exe "%1" %*
HKEY_LOCAL_MACHINE\Software\Classes\Python.NoConFile\shell\open\command
    (Default)    REG_SZ    c:\Program Files\Python\2.7\pythonw.exe "%1" %*
HKEY_LOCAL_MACHINE\Software\Classes\Python.CompiledFile\shell\open\command
    (Default)    REG_SZ    c:\Program Files\Python\2.7\python.exe "%1" %*

----- HKEY_CURRENT_USER -----
ERROR: The system was unable to find the specified registry key or value.
ERROR: The system was unable to find the specified registry key or value.
ERROR: The system was unable to find the specified registry key or value.
ERROR: The system was unable to find the specified registry key or value.
ERROR: The system was unable to find the specified registry key or value.
ERROR: The system was unable to find the specified registry key or value.
ERROR: The system was unable to find the specified registry key or value.

我还运行 Process Monitor(感谢Maximusa.py )来查看从命令行运行时正在读取哪些注册表项。我已将过滤器设置为仅包含cmd.exe用于运行a.py文件的 pid 并排除类型的操作RegCloseKey

"Operation","Path","Result","Detail"
"RegOpenKey","HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKLM\SOFTWARE\Microsoft\WINDOWS NT\CURRENTVERSION\AppCompatFlags\Custom\a.py","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer","SUCCESS","Desired Access: Query Value"
"RegQueryValue","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MaximizeApps","NAME NOT FOUND","Length: 144"
"RegOpenKey","HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer","SUCCESS","Desired Access: Query Value"
"RegQueryValue","HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\MaximizeApps","NAME NOT FOUND","Length: 144"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\Drive\shellex\FolderExtensions","NAME NOT FOUND","Desired Access: Enumerate Sub Keys"
"RegOpenKey","HKCR\Drive\shellex\FolderExtensions","SUCCESS","Desired Access: Enumerate Sub Keys"
"RegQueryKey","HKCR\Drive\shellex\FolderExtensions","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\Drive\shellex\FolderExtensions","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegEnumKey","HKCR\Drive\shellex\FolderExtensions","SUCCESS","Index: 0, Name: {fbeb8a05-beee-4442-804e-409d6c4515e9}"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\Drive\shellex\FolderExtensions\{fbeb8a05-beee-4442-804e-409d6c4515e9}","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCR\Drive\shellex\FolderExtensions\{fbeb8a05-beee-4442-804e-409d6c4515e9}","SUCCESS","Desired Access: Query Value"
"RegQueryKey","HKCR\Drive\shellex\FolderExtensions\{fbeb8a05-beee-4442-804e-409d6c4515e9}","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\Drive\shellex\FolderExtensions\{fbeb8a05-beee-4442-804e-409d6c4515e9}","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegQueryValue","HKCR\Drive\shellex\FolderExtensions\{fbeb8a05-beee-4442-804e-409d6c4515e9}\DriveMask","SUCCESS","Type: REG_DWORD, Length: 4, Data: 32"
"RegEnumKey","HKCR\Drive\shellex\FolderExtensions","NO MORE ENTRIES","Index: 1, Length: 288"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\.py","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCR\.py","SUCCESS","Desired Access: Query Value"
"RegQueryKey","HKCR\.py","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\.py","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegQueryValue","HKCR\.py\(Default)","SUCCESS","Type: REG_SZ, Length: 26, Data: Python.File "
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\.py\OpenWithProgids","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\.py\OpenWithProgids","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\OpenWithProgids","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\OpenWithProgids","SUCCESS","Query: Cached, SubKeys: 0, Values: 2"
"RegEnumValue","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\OpenWithProgids","SUCCESS","Index: 0, Length: 220"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\Python.File","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\Python.File","SUCCESS","Desired Access: Read"
"RegEnumValue","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\OpenWithProgids","SUCCESS","Index: 1, Length: 220"
"RegEnumValue","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\OpenWithProgids","NO MORE ENTRIES","Index: 2, Length: 220"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\.py","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\.py","SUCCESS","Desired Access: Read"
"RegOpenKey","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py","SUCCESS","Desired Access: Read"
"RegOpenKey","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py","SUCCESS","Desired Access: Read"
"RegOpenKey","HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice","NAME NOT FOUND","Desired Access: Query Value"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\Python.File ","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\Python.File ","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\.py","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\.py","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCR\.py","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\.py\shell","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\.py\shell","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\.py","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\.py","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\Unknown","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\Unknown","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\SystemFileAssociations\.py","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\SystemFileAssociations\.py","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\.py","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCR\.py","SUCCESS","Desired Access: Query Value"
"RegQueryKey","HKCR\.py","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\.py","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegQueryValue","HKCR\.py\PerceivedType","NAME NOT FOUND","Length: 144"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\SystemFileAssociations\.py","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCR\SystemFileAssociations\.py","NAME NOT FOUND","Desired Access: Query Value"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\*","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCU\Software\Classes\*","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\*\shell","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\*\shell","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCR\*\shell","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\*\shell","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegQueryValue","HKCR\*\shell\(Default)","NAME NOT FOUND","Length: 144"
"RegQueryKey","HKCR\*\shell","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\*\shell\open","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\*\shell\open","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCU\Software\Classes","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\AllFilesystemObjects","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\AllFilesystemObjects","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCR\AllFilesystemObjects","SUCCESS","Query: Name"
"RegOpenKey","HKCU\Software\Classes\AllFilesystemObjects\shell","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR\AllFilesystemObjects\shell","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKLM\Software\Microsoft\Windows\CurrentVersion\SideBySide","SUCCESS","Desired Access: Read"
"RegQueryValue","HKLM\COMPONENTS\PreferExternalManifest","NAME NOT FOUND","Length: 20"
"RegOpenKey","HKLM\Software\Microsoft\Windows\CurrentVersion\SideBySide\AssemblyStorageRoots","REPARSE","Desired Access: Enumerate Sub Keys"
"RegOpenKey","HKLM\COMPONENTS\AssemblyStorageRoots","NAME NOT FOUND","Desired Access: Enumerate Sub Keys"
"RegOpenKey","HKLM\Software\Microsoft\DirectUI","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKLM\Software\Microsoft\CTF\TIP\{0000897b-83df-4b96-be07-0fb58b01c4a4}\LanguageProfile\0x00000000\{0001bea3-ed56-483d-a2e2-aeae25577436}","SUCCESS","Desired Access: Read"
"RegQueryValue","HKLM\SOFTWARE\Microsoft\CTF\TIP\{0000897b-83df-4b96-be07-0fb58b01c4a4}\LanguageProfile\0x00000000\{0001bea3-ed56-483d-a2e2-aeae25577436}\Enable","SUCCESS","Type: REG_DWORD, Length: 4, Data: 1"
"RegOpenKey","HKCU","SUCCESS","Desired Access: Read"
"RegOpenKey","HKCU\Keyboard Layout\Toggle","SUCCESS","Desired Access: Read"
"RegQueryValue","HKCU\Keyboard Layout\Toggle\Language Hotkey","SUCCESS","Type: REG_SZ, Length: 4, Data: 3"
"RegQueryValue","HKCU\Keyboard Layout\Toggle\Layout Hotkey","SUCCESS","Type: REG_SZ, Length: 4, Data: 3"
"RegOpenKey","HKCU","SUCCESS","Desired Access: Read"
"RegOpenKey","HKCU\SOFTWARE\Microsoft\CTF\LangBarAddIn","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKLM\SOFTWARE\Microsoft\CTF\LangBarAddIn","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCU","SUCCESS","Desired Access: Read"
"RegOpenKey","HKCU\Software\Microsoft\CTF\DirectSwitchHotkeys","SUCCESS","Desired Access: Read"
"RegEnumKey","HKCU\Software\Microsoft\CTF\DirectSwitchHotkeys","NO MORE ENTRIES","Index: 0, Length: 288"
"RegOpenKey","HKLM\SOFTWARE\Microsoft\CTF\KnownClasses","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKLM\SOFTWARE\Microsoft\CTF\KnownClasses","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKLM\Software\Microsoft\Windows NT\CurrentVersion\FontSubstitutes","SUCCESS","Desired Access: Read"
"RegQueryValue","HKLM\SOFTWARE\Microsoft\WINDOWS NT\CURRENTVERSION\FontSubstitutes\Segoe UI","NAME NOT FOUND","Length: 144"
"RegOpenKey","HKLM\SOFTWARE\Microsoft\CTF\KnownClasses","NAME NOT FOUND","Desired Access: Read"
4

4 回答 4

6

一般来说,我会确保关联不会被当前用户的设置覆盖:

  • HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice
  • HKCU\Software\Classes\.py
  • HKCU\Software\Classes\Python.File

但是,当我在您的更新中阅读 Process Monitor 输出时,您的系统上似乎没有HKCU定义任何这些键。

我的下一步是在提升的cmd控制台中运行以下命令:

> assoc .PY=Python.File
> ftype Python.File=c:\Program Files\Python\2.7\python.exe "%1" %*

据我所知,这设置了系统关联,HKLM\Software\Classes没有做任何其他事情。根据您问题中的信息,这似乎已经正确设置。所以我不知道是什么assocftype做了什么来解决这个问题。

于 2012-11-11T15:10:54.720 回答
1

可能这可能会有所帮助(将“打开”作为默认动词)?

[HKEY_CLASSES_ROOT\Python.File\shell]
@="open"

还是这个(考虑到python.exe路径周围的配额)?

[HKEY_CLASSES_ROOT\Python.File\shell\open]
@="Open"

[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
@="\"c:\\Program Files\\Python\\2.7\\python.exe\" \"%1\" %*"
于 2012-11-10T22:11:35.980 回答
1

和 Piotr 一样,我一直在为这个问题苦苦挣扎。eriksyn 的建议使我找到了一个工作系统。

由于 Notepad2 总是打开我的“我想运行这个”调用,一定是我有一些东西将所有文件与 Notepad2 相关联,现在我知道在哪里看(我之前尝试运行进程监视器,但输出篇幅太大)。

我的注册表中有.py包含 Notepad2 内容的条目 。HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts我假设 Notepad2 自己添加了这些,或者我在配置时以某种方式这样做了。尤其:

> reg query HKCU\Software\...\Explorer\FileExts\.py\UserChoice
HKEY_CURRENT_USER\Software\...\Explorer\FileExts\.py\UserChoice
    Progid    REG_SZ    Applications\Notepad2.exe

使用此注册表项和值,尝试运行 Python 脚本只会打开 Notepad2。

如果没有这个注册表项,我的 Python 脚本实际上会执行,不再需要我输入“python <script>”来运行它们。

我很高兴。谢谢,堆栈溢出。

于 2013-09-28T21:41:52.677 回答
0

你不能用 GUI 的方式来做吗?

“打开方式...” -> 浏览 -> python.exe 并勾选“始终使用此程序...”

于 2012-11-10T22:42:45.463 回答