6

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 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

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_ROOT\Unknown\shell\notepad++\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++\notepad++ %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=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=

OUTPUT

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.

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","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"
Community
  • 1
  • 1
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
  • 1
    possible duplicate of [Making Python scripts run on Windows without specifying ".py" extension](http://stackoverflow.com/questions/9037346/making-python-scripts-run-on-windows-without-specifying-py-extension) – bgporter Nov 10 '12 at 21:44
  • @bgporter It's not duplicate of that question. I'm not talking about running Python script without writing its extension. – Piotr Dobrogost Nov 10 '12 at 22:09
  • Check and recreate if necessary your Python file [associations](http://stackoverflow.com/a/5586761/355230). – martineau Nov 10 '12 at 23:37
  • @Piotr -- did you actually read the link, or just look at the title? It shows you how to associate .py files with the python interpreter. – bgporter Nov 10 '12 at 23:37
  • @bgporter Have you noticed I've even **edited** that question in the past? You clearly have no clue what the problem I'm describing here is. – Piotr Dobrogost Nov 10 '12 at 23:42
  • @martineau As you can see from the output of `reg query...` commands I cited in my question, my associations are set properly. – Piotr Dobrogost Nov 11 '12 at 00:00
  • you could try to install [pylauncher](https://bitbucket.org/vinay.sajip/pylauncher). It registers itself to run python files. It should get you working settings – jfs Nov 11 '12 at 00:17
  • @J.F.Sebastian Thanks for suggestion (I'm well aware of this app) but I'd like to know what's going on here. – Piotr Dobrogost Nov 11 '12 at 00:26
  • @PiotrDobrogost: If it fixes the issue you'll know at what settings you should look more closely. If it doesn't you'll know to look somewhere else. See [pylauncher screencast](http://www.red-dove.com/screencasts/launcher/launcher.html) – jfs Nov 11 '12 at 00:58
  • Since all else has failed, just reinstall Python. – martineau Nov 11 '12 at 08:17
  • Have you tried running `assoc .PY=Python.File` and `ftype Python.File=c:\Program Files\Python\2.7\python.exe "%1" %*` in an elevated console? – Eryk Sun Nov 11 '12 at 09:46
  • You could also check the user settings in `HKCU\Software\Classes`. – Eryk Sun Nov 11 '12 at 09:49
  • @eryksun This fixed the problem - please write it as an answer. However I still don't know what was broken as the output from my batch file is identical to the one before running `assoc` and `ftype`. I thought my batch file displays all registry keys which `assoc` and `ftype` could possibly modify... I guess I should inspect what registry keys are being written by these programs using *Process Monitor*. – Piotr Dobrogost Nov 11 '12 at 12:27

4 Answers4

6

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

  • HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice
  • HKCU\Software\Classes\.py
  • HKCU\Software\Classes\Python.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 Files\Python\2.7\python.exe "%1" %*

As far as I know, this sets the system association in HKLM\Software\Classes 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.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • Maybe the problem had something to do with 32bit/64bit *mismatch*. Although I made sure `cmd.exe` I was running was 64bit, when I run `python` later to report its exact version in my question the version information ended with `on win32` text. I can't reproduce it now - there's no `on win32` suffix any more. Additionally when looking at registry keys being modified by `ftype` command I see it modifies `Wow6432Node` counterparts, too. Maybe there was something wrong with `Wow6432Node` version of registry keys and `ftype` fixed this. However I remember 32bit keys were the same as 64bit ones... – Piotr Dobrogost Nov 11 '12 at 23:14
2

I've been struggling with this problem for a while, same as Piotr. The suggestion by eriksyn led me to a working system.

Since Notepad2 was always opening my "I-want-to-run-this" invocations, it must be that I have something associating all files with Notepad2, and now I knew where to look (I tried running Process Monitor previously, but the output was too voluminous).

I have .py entries with Notepad2 contents in HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts in my registry. I assume either Notepad2 added these itself, or I did this somehow when configuring things. In particular:

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

With this registry key and value, trying to run a Python script just opens Notepad2.

Without this registry key, my Python scripts actually execute, no longer requiring me to type "python <script>" to run them.

I'm happy. Thanks, Stack Overflow.

1

May be this may help (make "open" as default verb)?

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

or this (take into account quotas around python.exe path)?

[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\" %*"
Maximus
  • 10,751
  • 8
  • 47
  • 65
  • Last chance ;) Run Process Monitor and try to run `a` in cmd prompt. Analyze log, I believe it may give you a hint – Maximus Nov 10 '12 at 22:35
  • Manually tell python to run my script from registry: "C:\Python27\python.exe" "C:\Python27\tpt.py" "%1"` –  Feb 22 '16 at 11:00
0

Can't you just do this the GUI way?

"Open with..." -> browse -> python.exe and tick "always use this program..."

Eric
  • 95,302
  • 53
  • 242
  • 374
  • When I choose *Open with...* I get the same error as shown in my update. See [Choose Default Program dialog does not show up after choosing Open with entry in context menu](http://superuser.com/questions/69193/) – Piotr Dobrogost Nov 10 '12 at 23:53