La ejecución del script Python desde la línea de comandos abre el script en el editor de texto predeterminado en lugar de ejecutar el script

Tengo archivos con extensión .py asociados con el intérprete de Python. Sin embargo, cuando escribo el nombre de dicho archivo en la línea de comando y presiono ENTER el archivo se abre en el editor de texto predeterminado (Notepad ++) en lugar de ejecutarse con el intérprete de Python asociado. ¿Por qué?

 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 

Estoy en Windows Vista x64 con Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32

ACTUALIZAR

Después de eliminar HKEY_CLASSES_ROOT\Unknown\shell\notepad++\command clave de HKEY_CLASSES_ROOT\Unknown\shell\notepad++\command (¿Creé esta clave en el pasado en mi búsqueda para asociar archivos sin extensión con Notepad ++? Vea ¿Cómo configurar el progtwig predeterminado para abrir archivos sin una extensión en Windows? ) Que tenía el valor de C:\Program Files (x86)\Notepad++\notepad++ %1 Recibo este error:

Este archivo no tiene un progtwig asociado para realizar esta acción. Crear una asociación en el panel de control Establecer asociaciones.

ACTUALIZACIÓN 2

Para mostrar todas las claves de registro que normalmente usa el sistema para averiguar qué progtwig está asociado con la extensión .py , ejecuto el siguiente archivo por lotes ( 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= 

SALIDA

 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. 

También ejecuto Process Monitor (gracias Maximus ) para ver qué claves de registro se están leyendo al ejecutar a.py desde la línea de comandos. He configurado el filtro para incluir solo el pid de cmd.exe utilizado para ejecutar a.py archivo.py y excluir operaciones de tipo 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" 

En general, me aseguraría de que la configuración no se anule a la asociación para el usuario actual:

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

Sin embargo, cuando leí el resultado del Monitor de proceso en su actualización, no parece que ninguna de estas claves HKCU esté definida en su sistema.

Mi siguiente paso sería ejecutar lo siguiente en una consola cmd elevada:

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

Por lo que sé, esto establece la asociación del sistema en HKLM\Software\Classes y no hace nada más. Según la información de su pregunta, parece que esto ya se configuró correctamente. Así que no sé qué assoc ni assoc ni ftype para solucionar el problema.

Puede ser que esto pueda ayudar (hacer “abierto” como verbo predeterminado)?

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

¿O esto (tener en cuenta las cuotas alrededor de la ruta 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\" %*" 

He estado luchando con este problema por un tiempo, igual que Piotr. La sugerencia de eriksyn me llevó a un sistema de trabajo.

Dado que Notepad2 siempre estaba abriendo mis invocaciones “Quiero-ejecutar-esto”, debe ser que tengo algo que asocia todos los archivos con Notepad2, y ahora sabía dónde buscar (intenté ejecutar Process Monitor anteriormente, pero la salida era demasiado voluminoso).

Tengo entradas .py con contenido de Notepad2 en HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts en mi registro. Supongo que Notepad2 agregó estos mismos, o lo hice de alguna manera al configurar las cosas. En particular:

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

Con esta clave de registro y este valor, al intentar ejecutar un script de Python solo se abre Notepad2.

Sin esta clave de registro, mis scripts de Python realmente se ejecutan, ya no es necesario que escriba “python

Estoy feliz. Gracias, desbordamiento de stack.

¿No puedes hacer esto de la manera GUI?

“Abrir con …” -> navegar -> python.exe y marcar “usar siempre este progtwig …”