Python脚本不适用于双击

Python script doesn#39;t work with double click(Python脚本不适用于双击)
本文介绍了Python脚本不适用于双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的问题,但我无法在旧答案中找到解决方案.当我双击 python 脚本时,我可以看到一个提示闪烁,但没有任何反应.如果我用 IDLE 打开相同的脚本并运行它,一切正常.为了确保脚本没有正确执行,我制作了一个这样的测试脚本:

I have a very basic problem, but I cannot find a solution in older answers. When I double click on a python script, I can see a prompt flashing but nothing happens. If I open the same script with IDLE and run it, everything works fine. To be sure the script was not executing propoerly, I made a test script like this:

def main():
   files = open('test.txt','a')
   files.write('this is a test')

如果通过空闲启动,简单脚本会写入文件,但如果我双击它,则不会发生任何事情.我尝试使用 .py 和 pyw 扩展名以及不止一台(Windows)电脑.我将python文件夹添加到路径中无济于事.

The simple script write the file if launched thru idle, but nothing happens if if I double click it. I tried with both .py and pyw extension and in more than one (windows) pc. I added the python folder to the path with no avail.

谢谢!

推荐答案

确保脚本包含这段代码:

Make sure that the script includes this snippet of code:

if __name__ == "__main__":
    # call your code here
    main()

这是从命令行运行的脚本的执行入口点,例如 C/C++/Java/C# 中的 main() 函数.在 this 帖子中了解更多信息.

That's the execution entry point for a script running from the command line, like the main() function in C/C++/Java/C#. Read more about it in this post.

另外,不要忘记显而易见的 - 为脚本提供正确的执行权限,确保 python.exe 命令在 %PATH% 中可用环境变量等.

Also, don't forget the obvious - give the right execution permissions to the script, make sure that the python.exe command is available in the %PATH% environment variable, and so on.

这篇关于Python脚本不适用于双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)