Python script doesn't work with double click

假装没事ソ 提交于 2019-12-19 03:40:13

问题


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')

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.

Thank you!


回答1:


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#. 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.



来源:https://stackoverflow.com/questions/18234908/python-script-doesnt-work-with-double-click

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