简体   繁体   English

双击Python脚本不起作用

[英]Python script doesn't work with double click

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. 当我双击python脚本时,我可以看到提示闪烁但没有任何反应。 If I open the same script with IDLE and run it, everything works fine. 如果我用IDLE打开相同的脚本并运行它,一切正常。 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')

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. 我尝试使用.py和pyw扩展以及多个(windows)pc。 I added the python folder to the path with no avail. 我将python文件夹添加到路径中但没有用。

Thank you! 谢谢!

Make sure that the script includes this snippet of code: 确保脚本包含以下代码段:

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

That's the execution entry point for a script running from the command line, like the main() function in C/C++/Java/C#. 这是从命令行运行的脚本的执行入口点,就像C / C ++ / Java / C#中的main()函数一样。 Read more about it in this post. 这篇文章中阅读更多相关信息。

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. 另外,不要忘记显而易见的事情 - 为脚本提供正确的执行权限,确保%PATH%环境变量中的python.exe命令可用,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM