Solve the problem of double click running seconds of python file

  • 2021-07-01 07:43:22
  • OfStack

Today, I wrote an python script for my colleagues to deal with Excel form. Double-click it on my computer to run normally, but it always retreats in seconds on my colleagues' computers. After studying for a long time, I finally solved it. Write it out and share it with you.

Cause analysis of py file second return:

First, make sure your computer has an python interpreter installed. If you don't, you can't run. py-ending files. Attach the installation method. After having python interpreter, you need to set the opening mode of. py file to python. exe. (Path 1 is generally C:\ Users\ Your User Name\ AppData\ Local\ Programs\ Python\ Python. exe)

Then consider the business of the code itself. If your code runs to the ground in one way, it will definitely retreat in seconds, because it takes 0.0 seconds for the computer to run these codes. If you add an infinite loop to your code logic, the black box of cmd will be in an open state, or you can add code such as sleep or input that can block the running of the program, which can also solve the problem of py file closing in seconds (this method was basically used when browsing related solutions on the Internet yesterday).

PS: There are infinite loops in my code, and there are methods in input, so exclude Reason 1.

One important point is to consider the module problem. For example, when my code started to import the module, there was such a sentence as import pandas as pd. My computer had the package pandas, but my colleague didn't have it in his computer, so the script returned in seconds when it arrived on his computer.

There is another problem, that is, the environment variable of python interpreter. My colleague's computer has double python interpreters, both 2.7 and 3.6, while my script is written in 3.6. His computer takes 2.7 first to open py files, which also needs to be considered. (How to configure environment variables for multiple python versions, leaving a post-position code)

After the above four reasons were eliminated one by one, my py script ran happily on my colleague's computer. (Problem situation may not be the same, but the general principle is the same, I hope to help you)


Related articles: