[英]Pyinstaller: Executable file opened with built in function in python closes after 1 sec when i make .exe file with pyinstaller
我在python中制作程序,以打开path给出的一些可执行文件。 我用:
os.startfile(path)
启动程序。 当我在IDLE中运行script.py时,它工作正常,但是当我运行pyinstaller来创建.exe文件时,我要打开的程序几乎立即开始打开并关闭。 我尝试使用不同的功能,例如subprocess.Popen(path)
,它执行相同的操作,在1秒钟后打开和关闭程序。 有人能帮我吗? python函数或pyinstaller甚至Windows 10中是否存在问题?
问题是您的代码很可能只是运行,然后窗口关闭。
您可以将import time
添加到您的导入和time.sleep(x)
或者在运行后将窗口保持打开状态x秒
在做任何事情之前,请通读全文!
好吧,试试这个:
将其放在一个名为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)
现在,从您从exe调用的文件中获取代码,假设其名称为run.py,并将其放在另一个文件中,例如code.py。
确保start.py与run.py位于同一文件夹中
在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)
因此,当您单击exe时,它将打开run.py,该文件告诉start.py打开包含程序的code.py。
实际上,您可以合并start.py和run.py,但如果它们是分开的,则可以重用start.py。
要么...
将import os
添加到exe调用的程序中。 将os.system("pause")
添加到程序的末尾。
我不确定这是否可以在无限运行的程序上运行...但是请先尝试这样做以节省时间。
更多信息:
在Microsoft Windows中执行时如何立即停止Python关闭
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.