Python 没有打开 .py 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16072837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:42:53  来源:igfitidea点击:

Python is not opening .py files

pythonscripting

提问by gadmaget

I'm trying to open a .py script on my computer but it unfortunately keeps opening in Notepad. I've tried changing the associations on my computer but even when I browse to python.exe and click it, it won't show up as a usable program (interestingly, pythonw.exe can be associated with it but nothing opens when I click on the file that's associated to pythonw). Is there any way I can fix this issue? If not, how can I run my script from the Python interpreter by hand?

我正在尝试在我的计算机上打开一个 .py 脚本,但不幸的是它一直在记事本中打开。我已经尝试更改计算机上的关联,但即使我浏览到 python.exe 并单击它,它也不会显示为可用程序(有趣的是,pythonw.exe 可以与之关联,但单击时没有任何打开)在与 pythonw 关联的文件上)。有什么办法可以解决这个问题吗?如果没有,我如何手动从 Python 解释器运行我的脚本?

Also, I've tried reinstalling Python, and that didn't help.

另外,我尝试重新安装 Python,但没有帮助。

采纳答案by Markus M.

The standard way of executing a python script on Windows is calling the python interpreter with the script name as parameter on the (DOS) command line. It is necessary though that the path to python.exe is included in your Path system variable.

在 Windows 上执行 python 脚本的标准方法是在 (DOS) 命令行上使用脚本名称作为参数调用 python 解释器。尽管 python.exe 的路径包含在您的 Path 系统变量中是必要的。

python myprogram.py

回答by Eric

I used IDLE in the past to run scripts I was working on. http://docs.python.org/2/library/idle.html

我过去使用 IDLE 来运行我正在处理的脚本。 http://docs.python.org/2/library/idle.html

From IDLE's Editmenu you should be able to run the open script.

从 IDLE 的Edit菜单中,您应该能够运行打开的脚本。

回答by user2293115

If you are on a Windows OS with IDLE installed, right-clicking a .py file should offer you the option to edit in IDLE.

如果您使用的是安装了 IDLE 的 Windows 操作系统,右键单击 .py 文件应该会为您提供在 IDLE 中进行编辑的选项。

回答by Todd

You can check the registry to see what is associated with .py and .pyw files. For me .py is associated with Python.File, which uses this for its shell\open command:

您可以检查注册表以查看与 .py 和 .pyw 文件关联的内容。对我来说,.py 与 Python.File 相关联,它在 shell\open 命令中使用它:

"C:\Python27\python.exe" "%1" %*

Similarly, .pyw is associated with Python.NoConFile which opens with pythonw.exe. Look for these extensions and file types as keys under HKEY_CLASSES_ROOT.

同样,.pyw 与 Python.NoConFile 相关联,后者通过 pythonw.exe 打开。在 HKEY_CLASSES_ROOT 下查找这些扩展名和文件类型作为键。

You can also use the ASSOC and FTYPE commands from a console window to check these:

您还可以从控制台窗口使用 ASSOC 和 FTYPE 命令来检查这些:

C:\projects>assoc .py
.py=Python.File

C:\projects>ftype python.file
python.file="C:\Python27\python.exe" "%1" %*

They also have command line variations to allow you to set new values. Check their help screens for more info, and search Stack Overflow for numerous examples.

它们还具有命令行变体,可让您设置新值。查看他们的帮助屏幕以获取更多信息,并在 Stack Overflow 中搜索大量示例。