简体   繁体   English

pyinstaller:当我用pyinstaller制作.exe文件时,在python中使用内置函数打开的可执行文件关闭1秒

[英]Pyinstaller: Executable file opened with built in function in python closes after 1 sec when i make .exe file with pyinstaller

I make program in python to open some executable file given by path. 我在python中制作程序,以打开path给出的一些可执行文件。 I use: 我用:

os.startfile(path)

to start program. 启动程序。 When i run script.py in IDLE it works fine, but when i make .exe file with pyinstaller when i run it that program i want to open starts to open and closes almost immediately. 当我在IDLE中运行script.py时,它工作正常,但是当我运行pyinstaller来创建.exe文件时,我要打开的程序几乎立即开始打开并关闭。 I tried with different functions like subprocess.Popen(path) and it does the same thing, open and close program after 1 second. 我尝试使用不同的功能,例如subprocess.Popen(path) ,它执行相同的操作,在1秒钟后打开和关闭程序。 Can someone help me? 有人能帮我吗? Is there a problem in python functions or in pyinstaller or even windows 10? python函数或pyinstaller甚至Windows 10中是否存在问题?

The problem is that your code most likely just runs and then the window closes. 问题是您的代码很可能只是运行,然后窗口关闭。

You can add import time to your imports and time.sleep(x) or something to keep the window open for x seconds after you run 您可以将import time添加到您的导入和time.sleep(x)或者在运行后将窗口保持打开状态x秒

Read all the way through before doing anything! 在做任何事情之前,请通读全文!

Ok so try this: 好吧,试试这个:

Put this in a python file called start.py: 将其放在一个名为start.py的python文件中:

import os
import subprocess

#py_command - whatever opens python in cmd
#path - must be full path of file you want to call

def StartPythonScript(path, py_command):    
    command = 'start cmd /k ' + py_command + " " + path
    subprocess.run(command, shell=True)

Now, take the code from file you are calling from the exe, let's say its name is run.py, and put it in a different file, say code.py. 现在,从您从exe调用的文件中获取代码,假设其名称为run.py,并将其放在另一个文件中,例如code.py。

Make sure start.py is in the same folder as run.py 确保start.py与run.py位于同一文件夹中

In run.py, put this: 在run.py中,输入以下内容:

import start

cmd = "py" #change this to whatever opens python in cmd
path = "code.py" #change to the full path of code.py
start.StartPythonScript(path, cmd)

So when you click on the exe, it opens run.py which tells start.py to open code.py, which contains your program. 因此,当您单击exe时,它将打开run.py,该文件告诉start.py打开包含程序的code.py。

You can actually merge start.py and run.py, but you can reuse start.py if they are seperate. 实际上,您可以合并start.py和run.py,但如果它们是分开的,则可以重用start.py。

OR... 要么...

Add import os to your program that is called by the exe. import os添加到exe调用的程序中。 Add os.system("pause") to the end of your program. os.system("pause")添加到程序的末尾。

I'm not sure if this will work on an infinitely running program...but try this first to save time. 我不确定这是否可以在无限运行的程序上运行...但是请先尝试这样做以节省时间。

More info: 更多信息:

How to stop Python closing immediately when executed in Microsoft Windows 在Microsoft Windows中执行时如何立即停止Python关闭

Good luck! 祝好运!

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

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