简体   繁体   English

子进程不适用于 pyinstaller

[英]Subprocess does not work with pyinstaller

I have the following sample script that works fine from Spyder.我有以下在 Spyder 上运行良好的示例脚本。 As an example I used Windows command dir /w .例如,我使用了 Windows 命令dir /w My real issue is an external utility that I need to launch, but this simple example shows the same issue:我真正的问题是我需要启动一个外部实用程序,但这个简单的示例显示了同样的问题:

import tkinter as tk
from tkinter import *

scriptpath=os.path.abspath(os.path.dirname(__file__))

def doit():
    cmdlst = ["dir", "/w"]
    foutput=scriptpath + "\\" + "result.txt"
    
    if os.path.exists(foutput):
      os.remove(foutput)
    with open(foutput, "w") as fresult:  
      result = subprocess.run(cmdlst, stdout=fresult, text=True, shell=True)


myDia = tk.Tk()
bt1=tk.Button(myDia, text="DoIt", command=doit, width=16, height=2)
bt1.grid(row=0, column=0, sticky='W', padx=0, pady=0)

myDia.mainloop()

When I compile it from the Anaconda prompt with:当我从 Anaconda 提示符编译它时:

pyinstaller --onefile subproc.pyw

it will generate the exe.它将生成exe。 When running it, it will launch the dialog.运行它时,它将启动对话框。 When pressing the button it will create an empty file "result.txt", but there is no content and no error.按下按钮时会创建一个空文件“result.txt”,但没有内容也没有错误。 The program just hangs.该程序只是挂起。

Why doesn't the compiled exe give the expected result like the .pyw file in Spyder?为什么编译后的 exe 没有像 Spyder 中的.pyw文件那样给出预期的结果? Please note that the Tkinter dialog seems to be relevant in causing this issue.请注意,Tkinter 对话框似乎与导致此问题有关。 Removing the Tkinter dialog and renaming the file to.py results in a working compiled program.删除 Tkinter 对话框并将文件重命名为 .py 会生成一个工作编译程序。

Thanks for checking.感谢您的检查。 It triggered me to test with Python 3.9 and Pyinstaller 5.1 and I can confirm that this works as you described.它促使我使用 Python 3.9 和 Pyinstaller 5.1 进行测试,我可以确认这如您所描述的那样工作。 So it must be some issue solved in a newer version (issue was detected with Python 3.7 and pyinstaller 4.1所以它一定是在较新版本中解决了一些问题(在 Python 3.7 和 pyinstaller 4.1 中检测到问题

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

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