简体   繁体   English

Pyinstaller 的 .exe 文件立即关闭

[英]Pyinstaller's .exe file closes immediately

I have a working python package that's a CLI tool and I wanted to convert it into a single .exe file to upload it to other package managers so I used Pyinstaller.我有一个工作 python 包,它是一个 CLI 工具,我想将它转换为单个.exe文件以将其上传到其他包管理器,所以我使用了 Pyinstaller。 After building the .exe file with this command:使用此命令构建.exe文件后:

pyinstaller -c --log-level=DEBUG main.py 2> build.txt --onefile --exclude-module=pytest --add-data "src;src"

I double-clicked the .exe file but it closed immediately but in that split second, I saw the expected output which is supposed to be the command-line interface so the .exe does work but not entirely.我双击了 .exe 文件,但它立即关闭了,但在那一瞬间,我看到了预期的输出,它应该是命令行界面,所以 .exe 确实可以工作,但并不完全。

main.py主文件

from src.Categorize_CLI.__main__ import main

if __name__ == "__main__":
    main()

.spec file .spec 文件

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[('src', 'src')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=['pytest'],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='main',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

Update更新

I got it working by dragging the main.exe file to a open command prompt and then pressed enter and it worked, and I got an error:我通过将 main.exe 文件拖到打开的命令提示符然后按下回车键,它工作了,但我得到了一个错误:

RuntimeError: 'package' is not installed. Try passing 'package_name' instead.
[15592] Failed to execute script 'main' due to unhandled exception!

It sounds like the script ran to end of file to fast for you to see.听起来脚本运行到文件末尾以快速供您查看。 You can confirm this by opening your terminal (cmd/poweshell in windows) and running your program like any other CLI tool.您可以通过打开终端(Windows 中的 cmd/poweshell)并像任何其他 CLI 工具一样运行程序来确认这一点。

cd path\to\exe
./exe -arguments

Since you launched it from an allready opened terminal it won't close when the script ends.由于您是从一个已经打开的终端启动它,因此脚本结束时它不会关闭。

If this is the problem you can solve it by adding如果这是问题,您可以通过添加来解决它

input("Continue...") # wait for user

Update更新

As @BokiX says pyinstaller can cause false positives with anti virus software.正如@BokiX 所说, pyinstaller可能会导致防病毒软件误报。 Try a diffrent one eg nuikta:尝试不同的,例如 nuikta:

pip install Nuikta

python -m nuikta main.py

installing python programs vs tradtitional programs安装 python 程序与传统程序

A tradtitional program installer is usualy a fancy zip file with some additianal feautures to setup a program for the relevant system it's on (eg make registry changes, download additianal files).传统的程序安装程序通常是一个带有一些附加功能的精美 zip 文件,用于为其所在的相关系统设置程序(例如,更改注册表、下载附加文件)。

A python program is just a python script that was "frozen" in state so it can run independently on a system without python or it's dependencies. python 程序只是一个处于“冻结”状态的 python 脚本,因此它可以在没有python或其依赖项的系统上独立运行。 once you have an exe it should just run without needing to be "installed".一旦你有一个exe ,它应该可以运行而不需要“安装”。

using console programs A console program is a program that is made to be exicuted from a terminal.使用控制台程序控制台程序是从终端执行的程序。 In modern use these are usualy so they can be run by a script or someone who finds typing is faster than using a GUI.在现代使用中,这些通常是由脚本或发现打字比使用 GUI 更快的人运行的。

Run the code in the cmd and not by clicking the exe在 cmd 中运行代码,而不是通过单击 exe

A pyinstaller exe "usually" closes when 1 - Theres an error in the code , 2 - The code execution finished , closes after displaying the output pyinstaller exe“通常”在以下情况下关闭:1 - 代码中有错误,2 - 代码执行完成,显示输出后关闭

Also as BokiX stated , pyinstaller exe's often get false flagged as malicious , so maybe add an exception to your anti virus.同样正如 BokiX 所说,pyinstaller exe 经常被错误地标记为恶意,因此可能会在您的防病毒软件中添加一个例外。

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

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