【问题标题】:Pyinstaller: Executable file opened with built in function in python closes after 1 sec when i make .exe file with pyinstallerPyinstaller:当我使用 pyinstaller 创建 .exe 文件时,在 python 中使用内置函数打开的可执行文件在 1 秒后关闭
【发布时间】:2019-05-02 18:48:00
【问题描述】:

我在 python 中编写程序来打开路径给出的一些可执行文件。我用:

os.startfile(path)

启动程序。当我在 IDLE 中运行 script.py 时它工作正常,但是当我使用 pyinstaller 制作 .exe 文件时,当我运行它时,我想要打开的程序开始打开并几乎立即关闭。我尝试了不同的功能,如subprocess.Popen(path),它做同样的事情,1 秒后打开和关闭程序。有人能帮我吗? python函数或pyinstaller甚至windows 10是否有问题?

【问题讨论】:

  • 不能把整个代码放到exe文件里吗?
  • 这种事情也发生在我身上。问题出在启动器中
  • 不是exe文件
  • 哪个启动器?
  • 我的意思是python默认启动器

标签: python pyinstaller


【解决方案1】:

问题是您的代码很可能只是运行然后窗口关闭。

您可以将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")

我不确定这是否适用于无限运行的程序...但请先尝试此操作以节省时间。

更多信息:

How to stop Python closing immediately when executed in Microsoft Windows

祝你好运!

【讨论】:

  • 不,我的程序可以无限运行。当我想运行其他程序时,它会继续工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 1970-01-01
  • 2021-11-02
  • 2016-12-13
相关资源
最近更新 更多