双击运行 Python 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41639740/
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-20 01:20:42  来源:igfitidea点击:

Running Python file by double-click

pythonbatch-filepython-idle

提问by Hyman

I have really annoying problem, I cannot run a Python file just by double-clicking.

我有一个很烦人的问题,我无法通过双击运行 Python 文件。

I have tried to set it to open the file with idle.batbut that only starts IDLE editor on double-click, it does not run the Python file.

我试图将它设置为打开文件,idle.bat但只在双击时启动 IDLE 编辑器,它不运行 Python 文件。

回答by OregonTrail

What version of Python do you have installed?

你安装了什么版本的Python?

You should write your own batch file to execute your python binary and your script.

您应该编写自己的批处理文件来执行您的 python 二进制文件和脚本。

For example, with a default Python 2.7 installation on Windows, this could be the entire contents of your script.

例如,对于 Windows 上的默认 Python 2.7 安装,这可能是脚本的全部内容。

myscript.bat:

myscript.bat

ECHO ON
REM A batch script to execute a Python script
SET PATH=%PATH%;C:\Python27
python yourscript.py
PAUSE

Save this file as "myscript.bat" (make sure it's not "myscript.bat.txt"), then double click it.

将此文件另存为“myscript.bat”(确保它不是“myscript.bat.txt”),然后双击它。

回答by Xiidozen

Right click the file, select open with. If you want to simply run the script, find python.exe and select it. If you want to debug with IDLE, find that executable and select it.

右键单击文件,选择打开方式。如果您只想运行脚本,请找到 python.exe 并选择它。如果要使用 IDLE 进行调试,请找到该可执行文件并选择它。

回答by ratna

right click on the file->open with->choose default program->more options->select python.exe file and click on.

右键单击文件->打开方式->选择默认程序->更多选项->选择python.exe文件并单击。

回答by James Koss

When I had both Py2 and Py3, and then removed the former, my script wouldn't run by double-clicking it either (but fine from console.) I realized my __pycache__folder (same directory as the script) was the issue. Problem solved when deleted.

当我同时拥有 Py2 和 Py3,然后删除前者时,我的脚本也不会通过双击它来运行(但从控制台很好)。我意识到我的__pycache__文件夹(与脚本相同的目录)是问题所在。删除后问题解决。

回答by Kalyan

I have the same issue. IDLE has the default python path to execute scripts. But when you install software like Anaconda they change the python path. So it doesn't work when we double-clicked the script(example.py) file. So, I decided to uninstall the Anaconda and it worked after uninstalling it. You no need to uninstall it you or may change the python path to default.

我有同样的问题。IDLE 具有执行脚本的默认 python 路径。但是当你安装像 Anaconda 这样的软件时,它们会改变 python 路径。所以当我们双击脚本(example.py)文件时它不起作用。所以,我决定卸载 Anaconda,卸载后它就可以工作了。您无需卸载它,也可以将 python 路径更改为默认值。