0

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. After building the .exe file with this command:

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.

main.py

from src.Categorize_CLI.__main__ import main

if __name__ == "__main__":
    main()

.spec file

# -*- 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:

RuntimeError: 'package' is not installed. Try passing 'package_name' instead.
[15592] Failed to execute script 'main' due to unhandled exception!
13
  • They are detected as a virus, they probably get deleted after a few tries right?
    – BokiX
    Commented May 2, 2022 at 16:46
  • What I recommend you is using Nuitka package because it helps with your files not being detected as a virus and it has pretty much the same commands as Pyinstaller, just read the documentation so you don't mess anything up.
    – BokiX
    Commented May 2, 2022 at 16:48
  • I ran the .exe file a couple times but it didn't get deleted. I will check out Nuitka hopefully that will solve my problem Commented May 2, 2022 at 16:50
  • Is your package name in the datas field in the spec file? I only see one. DId you try compiling to a folder first as the docs suggest? Also pause the anti virus for testing
    – medic17
    Commented May 3, 2022 at 7:12
  • @medic17 No that's the src folder which is outside the top level module Categorize_CLI. I added src folder as data because of a previous import error Commented May 3, 2022 at 9:17

2 Answers 2

1

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.

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. Try a diffrent one e.g. nuikta:

pip install Nuikta

python -m nuikta main.py

installing python programs vs tradtitional programs

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 (e.g. make registry changes, download additianal files).

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. once you have an exe it should just run without needing to be "installed".

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.

1
  • The main.exe file got detected as Trojan:Win32/Wacatac.B!ml and then got deleted Commented May 3, 2022 at 3:21
1

Run the code in the cmd and not by clicking the exe

A pyinstaller exe "usually" closes when 1 - Theres an error in the code , 2 - The code execution finished , closes after displaying the output

Also as BokiX stated , pyinstaller exe's often get false flagged as malicious , so maybe add an exception to your anti virus.

4
  • The main.exe file got detected as Trojan:Win32/Wacatac.B!ml and then got deleted Commented May 3, 2022 at 3:21
  • Add an exception to your anti virus so it does not delete
    – 0xApollyon
    Commented May 3, 2022 at 8:53
  • I disabled the antivirus and I got the program to work but I have to run this command to make it work: \path\to\exe [options] [commands] Commented May 3, 2022 at 9:20
  • Then thats something in your exe , cant help with that
    – 0xApollyon
    Commented May 6, 2022 at 15:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.