Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

windows - Running Python script from command line opens script in the default text editor instead of executing script

I have files with .py extension associated with Python interpreter. However when I type name of such a file at the command line and press ENTER the file is being opened in the default text editor (Notepad++) instead of being run with the associated Python interpreter. Why?

Z:1>where python
c:Program FilesPython2.7python.exe

Z:1>reg query HKCR.py /ve

HKEY_CLASSES_ROOT.py
    (Default)    REG_SZ    Python.File


Z:1>reg query HKCRPython.Fileshellopencommand /ve

HKEY_CLASSES_ROOTPython.Fileshellopencommand
    (Default)    REG_SZ    c:Program FilesPython2.7python.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

I'm on Windows Vista x64 using Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32

UPDATE

After deleting HKEY_CLASSES_ROOTUnknownshell otepad++command key (I created this key in the past in my quest to associate files without extension with Notepad++ - see How to set the default program for opening files without an extension in Windows?) which had value of C:Program Files (x86)Notepad++ otepad++ %1 I get this error:

This file does not have a program associated with it for performing this action. Create an association in the Set Associations control panel.

UPDATE 2

To display all registry keys used normally by system to find out what program is associated with .py extension I run the following batch file (python_assoc.query.bat):

@echo off
if "%~1" == "details" (
    set keypath=HKLMSoftwareClasses
    echo ----- HKEY_LOCAL_MACHINE -----
    call :query
    set keypath=HKCUSoftwareClasses
    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.Fileshellopencommand /ve
reg query %keypath%Python.NoConFileshellopencommand /ve
reg query %keypath%Python.CompiledFileshellopencommand /ve
goto :eof

:end
set keypath=

OUTPUT

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

HKEY_LOCAL_MACHINESoftwareClasses.py
    (Default)    REG_SZ    Python.File
HKEY_LOCAL_MACHINESoftwareClasses.pyw
    (Default)    REG_SZ    Python.NoConFile
HKEY_LOCAL_MACHINESoftwareClasses.pyc
    (Default)    REG_SZ    Python.CompiledFile
HKEY_LOCAL_MACHINESoftwareClasses.pyo
    (Default)    REG_SZ    Python.CompiledFile
HKEY_LOCAL_MACHINESoftwareClassesPython.Fileshellopencommand
    (Default)    REG_SZ    c:Program FilesPython2.7python.exe "%1" %*
HKEY_LOCAL_MACHINESoftwareClassesPython.NoConFileshellopencommand
    (Default)    REG_SZ    c:Program FilesPython2.7pythonw.exe "%1" %*
HKEY_LOCAL_MACHINESoftwareClassesPython.CompiledFileshellopencommand
    (Default)    REG_SZ    c:Program FilesPython2.7python.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.

I also run Process Monitor (thanks Maximus) to see what registry keys are being read when running a.py from command line. I have set filter to include only pid of cmd.exe used to run a.py file and to exclude operations of type RegCloseKey.

"Operation","Path","Result","Detail"
"RegOpenKey","HKCUSoftwareMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKLMSOFTWAREMicrosoftWINDOWS NTCURRENTVERSIONAppCompatFlagsCustoma.py","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorer","SUCCESS","Desired Access: Query Value"
"RegQueryValue","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerMaximizeApps","NAME NOT FOUND","Length: 144"
"RegOpenKey","HKLMSoftwareMicrosoftWindowsCurrentVersionExplorer","SUCCESS","Desired Access: Query Value"
"RegQueryValue","HKLMSOFTWAREMicrosoftWindowsCurrentVersionexplorerMaximizeApps","NAME NOT FOUND","Length: 144"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesDriveshellexFolderExtensions","NAME NOT FOUND","Desired Access: Enumerate Sub Keys"
"RegOpenKey","HKCRDriveshellexFolderExtensions","SUCCESS","Desired Access: Enumerate Sub Keys"
"RegQueryKey","HKCRDriveshellexFolderExtensions","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesDriveshellexFolderExtensions","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegEnumKey","HKCRDriveshellexFolderExtensions","SUCCESS","Index: 0, Name: {fbeb8a05-beee-4442-804e-409d6c4515e9}"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesDriveshellexFolderExtensions{fbeb8a05-beee-4442-804e-409d6c4515e9}","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCRDriveshellexFolderExtensions{fbeb8a05-beee-4442-804e-409d6c4515e9}","SUCCESS","Desired Access: Query Value"
"RegQueryKey","HKCRDriveshellexFolderExtensions{fbeb8a05-beee-4442-804e-409d6c4515e9}","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesDriveshellexFolderExtensions{fbeb8a05-beee-4442-804e-409d6c4515e9}","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegQueryValue","HKCRDriveshellexFolderExtensions{fbeb8a05-beee-4442-804e-409d6c4515e9}DriveMask","SUCCESS","Type: REG_DWORD, Length: 4, Data: 32"
"RegEnumKey","HKCRDriveshellexFolderExtensions","NO MORE ENTRIES","Index: 1, Length: 288"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses.py","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCR.py","SUCCESS","Desired Access: Query Value"
"RegQueryKey","HKCR.py","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses.py","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegQueryValue","HKCR.py(Default)","SUCCESS","Type: REG_SZ, Length: 26, Data: Python.File "
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses.pyOpenWithProgids","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR.pyOpenWithProgids","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.pyOpenWithProgids","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.pyOpenWithProgids","SUCCESS","Query: Cached, SubKeys: 0, Values: 2"
"RegEnumValue","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.pyOpenWithProgids","SUCCESS","Index: 0, Length: 220"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesPython.File","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCRPython.File","SUCCESS","Desired Access: Read"
"RegEnumValue","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.pyOpenWithProgids","SUCCESS","Index: 1, Length: 220"
"RegEnumValue","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.pyOpenWithProgids","NO MORE ENTRIES","Index: 2, Length: 220"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses.py","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR.py","SUCCESS","Desired Access: Read"
"RegOpenKey","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.py","SUCCESS","Desired Access: Read"
"RegOpenKey","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.py","SUCCESS","Desired Access: Read"
"RegOpenKey","HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.pyUserChoice","NAME NOT FOUND","Desired Access: Query Value"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesPython.File ","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCRPython.File ","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses.py","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR.py","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCR.py","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses.pyshell","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR.pyshell","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses.py","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR.py","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesUnknown","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCRUnknown","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesSystemFileAssociations.py","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCRSystemFileAssociations.py","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses.py","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCR.py","SUCCESS","Desired Access: Query Value"
"RegQueryKey","HKCR.py","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses.py","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegQueryValue","HKCR.pyPerceivedType","NAME NOT FOUND","Length: 144"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesSystemFileAssociations.py","NAME NOT FOUND","Desired Access: Query Value"
"RegOpenKey","HKCRSystemFileAssociations.py","NAME NOT FOUND","Desired Access: Query Value"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses*","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCUSoftwareClasses*","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses*shell","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR*shell","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCR*shell","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses*shell","NAME NOT FOUND","Desired Access: Maximum Allowed"
"RegQueryValue","HKCR*shell(Default)","NAME NOT FOUND","Length: 144"
"RegQueryKey","HKCR*shell","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClasses*shellopen","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCR*shellopen","NAME NOT FOUND","Desired Access: Read"
"RegQueryKey","HKCUSoftwareClasses","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesAllFilesystemObjects","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCRAllFilesystemObjects","SUCCESS","Desired Access: Read"
"RegQueryKey","HKCRAllFilesystemObjects","SUCCESS","Query: Name"
"RegOpenKey","HKCUSoftwareClassesAllFilesystemObjectsshell","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKCRAllFilesystemObjectsshell","NAME NOT FOUND","Desired Access: Read"
"RegOpenKey","HKLMSoftwareMicrosoftWindowsCurrentVersionSideBySide","SUCCESS","Desired Access: Read"
"RegQueryValue","HKLMCOMPONENTSPreferExt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In general, I'd make sure the association isn't being overridden by settings for the current user:

  • HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.pyUserChoice
  • HKCUSoftwareClasses.py
  • HKCUSoftwareClassesPython.File

However, as I read the Process Monitor output in your update, it doesn't appear that any of these HKCU keys are defined on your system.

My next step would be to run the following in an elevated cmd console:

> assoc .PY=Python.File
> ftype Python.File=c:Program FilesPython2.7python.exe "%1" %*

As far as I know, this sets the system association in HKLMSoftwareClasses and does nothing else. Based on the information in your question, it seems this was already set correctly. So I don't know what either assoc or ftype did to fix the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...