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

感情迁移 提交于 2019-11-29 07:11:09

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.

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

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.

Can't you just do this the GUI way?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!