Skip to content Skip to sidebar Skip to footer

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 defa

Solution 1:

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.


Solution 2:

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\" %*"

Solution 3:

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.


Solution 4:

Can't you just do this the GUI way?

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


Post a Comment for "Running Python Script From Command Line Opens Script In The Default Text Editor Instead Of Executing Script"