marcelotduarte / cx_Freeze

cx_Freeze creates standalone executables from Python scripts, with the same performance, is cross-platform and should work on any platform that Python itself works on.
https://marcelotduarte.github.io/cx_Freeze/
Other
1.32k stars 217 forks source link

Windows console-based application crashes immediately after opening #2515

Open CanYing0913 opened 2 months ago

CanYing0913 commented 2 months ago

Describe the bug I used the same python environment to freeze my program in Windows. The program runs well if running from source under the same python environment, but after freezing it, opening the executable will crash immediately.

Expected behavior My application is based on tkinter and base=console in setup.py, as I still need to see my helpful debugging messages. Previously, this setting worked fine - there was still GUI popping up. But now, it will open a console and quickly close itself without any debugging print messages in the console, which makes diagnosing the problem very difficult.

Desktop (please complete the following information):

Additional context setup.py:

from cx_Freeze import setup, Executable

build_exe_options = {
    "build_exe": "dist",
    "includes": [
        "caiman",
    ],
    # "packages": [
    # ],
    "include_files": [
        "config.json",
        "resource",
        "log"
    ],

}
build_msi_options = {}
build_mac_options = {}
build_appimage_options = {}

executables = [
    Executable(
        "main.py",
        base="console",
        # icon="icon.ico",
        target_name="Program",
        shortcut_name="Program",
        shortcut_dir="Program",
    )
]

setup(
    name="Program",
    version="0.5",
    description="Toolbox",
    options={
        "build_exe": build_exe_options,
        "bdist_msi": build_msi_options,
        "bdist_mac": build_mac_options,
        "bdist_appimage": build_appimage_options,
    },
    executables=executables,
)
marcelotduarte commented 1 month ago

I tested using a modified version of the tkinter sample with base="console" and a print(text) after line 11, and it worked, with the latest cx_Freeze 7.3.0.dev9

CanYing0913 commented 1 month ago

Hmm I am still seeing crashes on my side, though I didn't run on a test sample. I got some warning messages this time from an older commit (seconds later it crashes): QQ_1721980386379

I am also getting error code 2756 during msi installation.

Could you try my code base to see if there are any problems? It is here. Many thanks.

marcelotduarte commented 1 month ago

The error is related to tzdata/zoneinfo. Please check your conda list (mamba list?) if tzdata is installed.

Also you can use the sample tz to test if it is working. For me is it working.

I tried to clone your project, but I cannot run it with 'python main.py'. I got an error: FileNotFoundError: [Errno 2] No such file or directory: '../data/scale_cluster_average.txt'

CanYing0913 commented 1 month ago

Thanks for providing the sample! I tried on it and it seems that one will also crash soon after starting it. Here is my list of tzdata in my mamba env:

mamba list tzdata
# packages in environment at C:\Users\PC\mambaforge\envs\myenv:
#
# Name                    Version                   Build  Channel
python-tzdata            2024.1             pyhd8ed1ab_0    conda-forge
tzdata                         2024a                h0c530f3_0    conda-forge

Are those versions supported?

marcelotduarte commented 1 month ago

I'm using the default installed, tzdata, which is the same version as yours, and the sample works. I do not have python-tzdata. Could you remove it and make a new test with the sample?

CanYing0913 commented 1 month ago

Just created a new environment and tried on it with command pip install -U cx_Freeze "backports.zoneinfo;python_version<'3.9'" "tzdata;sys_platform=='win32'" and it still fails. However, I noticed there are a couple of warnings at the beginning. Are those the cause of the issue?

python setup.py build
running build
running build_exe
creating directory D:\CanYing\Code\cx_Freeze\samples\tz\build\exe.win-amd64-3.8
copying C:\Users\PC\mambaforge\envs\test\python38.dll -> D:\CanYing\Code\cx_Freeze\samples\tz\build\exe.win-amd64-3.8\python38.dll
copying C:\Users\PC\mambaforge\envs\test\zlib.dll -> D:\CanYing\Code\cx_Freeze\samples\tz\build\exe.win-amd64-3.8\zlib.dll
WARNING: cannot find 'api-ms-win-core-rtlsupport-l1-2-2.dll'
WARNING: cannot find 'api-ms-win-core-processthreads-l1-1-3.dll'
WARNING: cannot find 'api-ms-win-core-processthreads-l1-1-2.dll'
WARNING: cannot find 'api-ms-win-core-registry-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-heap-l2-1-0.dll'
WARNING: cannot find 'api-ms-win-core-memory-l1-1-1.dll'
WARNING: cannot find 'api-ms-win-core-memory-l1-1-2.dll'
WARNING: cannot find 'api-ms-win-core-synch-l1-2-1.dll'
WARNING: cannot find 'api-ms-win-core-file-l1-2-2.dll'
WARNING: cannot find 'api-ms-win-core-file-l1-2-4.dll'
WARNING: cannot find 'api-ms-win-core-file-l1-2-1.dll'
WARNING: cannot find 'api-ms-win-core-delayload-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-io-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-io-l1-1-1.dll'
WARNING: cannot find 'api-ms-win-core-job-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-threadpool-legacy-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-threadpool-private-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-largeinteger-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-libraryloader-l1-2-3.dll'
WARNING: cannot find 'api-ms-win-core-libraryloader-l1-2-2.dll'
WARNING: cannot find 'api-ms-win-core-libraryloader-l1-2-0.dll'
WARNING: cannot find 'api-ms-win-core-libraryloader-l1-2-1.dll'
WARNING: cannot find 'api-ms-win-core-libraryloader-l2-1-0.dll'
WARNING: cannot find 'api-ms-win-core-namedpipe-l1-2-2.dll'
WARNING: cannot find 'api-ms-win-core-namedpipe-l1-2-1.dll'
WARNING: cannot find 'api-ms-win-core-datetime-l1-1-1.dll'
WARNING: cannot find 'api-ms-win-core-datetime-l1-1-2.dll'
WARNING: cannot find 'api-ms-win-core-sysinfo-l1-2-0.dll'
WARNING: cannot find 'api-ms-win-core-sysinfo-l1-2-1.dll'
WARNING: cannot find 'api-ms-win-core-sysinfo-l1-2-3.dll'
WARNING: cannot find 'api-ms-win-core-processsnapshot-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-processenvironment-l1-2-0.dll'
WARNING: cannot find 'api-ms-win-core-debug-l1-1-1.dll'
WARNING: cannot find 'api-ms-win-core-errorhandling-l1-1-3.dll'
WARNING: cannot find 'api-ms-win-security-base-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-security-base-l1-2-0.dll'
WARNING: cannot find 'api-ms-win-security-appcontainer-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-comm-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-realtime-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-wow64-l1-1-1.dll'
WARNING: cannot find 'api-ms-win-core-wow64-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-wow64-l1-1-3.dll'
WARNING: cannot find 'api-ms-win-core-systemtopology-l1-1-1.dll'
WARNING: cannot find 'api-ms-win-core-systemtopology-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-processtopology-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-namespace-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-file-l2-1-2.dll'
WARNING: cannot find 'api-ms-win-core-file-l2-1-1.dll'
WARNING: cannot find 'api-ms-win-core-file-l2-1-3.dll'
WARNING: cannot find 'api-ms-win-core-xstate-l2-1-0.dll'
WARNING: cannot find 'api-ms-win-core-xstate-l2-1-1.dll'
WARNING: cannot find 'api-ms-win-core-xstate-l2-1-2.dll'
WARNING: cannot find 'api-ms-win-core-localization-l2-1-0.dll'
WARNING: cannot find 'api-ms-win-core-normalization-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-heap-obsolete-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-fibers-l2-1-0.dll'
WARNING: cannot find 'api-ms-win-core-fibers-l2-1-1.dll'
WARNING: cannot find 'api-ms-win-core-localization-private-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-sidebyside-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-appcompat-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-windowserrorreporting-l1-1-1.dll'
WARNING: cannot find 'api-ms-win-core-windowserrorreporting-l1-1-2.dll'
WARNING: cannot find 'api-ms-win-core-windowserrorreporting-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-windowserrorreporting-l1-1-3.dll'
WARNING: cannot find 'api-ms-win-core-console-l1-2-1.dll'
WARNING: cannot find 'api-ms-win-core-console-l2-1-0.dll'
WARNING: cannot find 'api-ms-win-core-console-l2-2-0.dll'
WARNING: cannot find 'api-ms-win-core-console-l3-2-0.dll'
WARNING: cannot find 'api-ms-win-core-psapi-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-psapi-ansi-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-apiquery-l1-1-0.dll'
WARNING: cannot find 'api-ms-win-core-delayload-l1-1-1.dll'
WARNING: cannot find 'api-ms-win-core-appcompat-l1-1-1.dll'
WARNING: cannot find 'ext-ms-win-oobe-query-l1-1-0.dll'
WARNING: cannot find 'ext-ms-win-packagevirtualizationcontext-l1-1-0.dll'
copying C:\Users\PC\mambaforge\envs\test\python3.dll -> D:\CanYing\Code\cx_Freeze\samples\tz\build\exe.win-amd64-3.8\python3.dll
copying C:\Users\PC\mambaforge\envs\test\Lib\site-packages\cx_Freeze\bases\console-cpython-38-win_amd64.exe -> D:\CanYing\Code\cx_Freeze\samples\tz\build\exe.win-amd64-3.8\test_tz.exe
copying C:\Users\PC\mambaforge\envs\test\Lib\site-packages\cx_Freeze\initscripts\frozen_application_license.txt -> D:\CanYing\Code\cx_Freeze\samples\tz\build\exe.win-amd64-3.8\frozen_application_license.txt
Stamped: D:\CanYing\Code\cx_Freeze\samples\tz\build\exe.win-amd64-3.8\test_tz.exe
writing zip file D:\CanYing\Code\cx_Freeze\samples\tz\build\exe.win-amd64-3.8\lib\library.zip
.......
marcelotduarte commented 1 month ago

I tested with python-tzdata installed and get an error on stderr. Removed it and works. The warnings in the build about 'api-ms-win-*' are ok. To include the files please check the FAQ.

Uninstalling python-tzdata removes the message and your app works?

CanYing0913 commented 1 month ago

It didn't work unfortunately. I didn't remove python-tzdata because mamba will remove all my packages that way, which is strange. In my newly created test environment, I only have tzdata installed:

mamba list tzdata
mamba list tzdata
# packages in environment at C:\Users\PC\mambaforge\envs\test:
#
# Name                    Version                   Build  Channel
tzdata                    2024.1                   pypi_0    pypi

But it still does not work. P.S. My code in my repo is on branch v0.5 instead of master branch. Sorry for the confusion

marcelotduarte commented 1 month ago

P.S. My code in my repo is on branch v0.5 instead of master branch. Sorry for the confusion

I tried it, and it does not have 'data/scale_cluster_average.txt'. I cannot test.

CanYing0913 commented 1 month ago

Oops sorry about that. I forgot to comment out some incomplete work. Can you try now? To create the environment, I used this command:

mamba create -n test_env -c conda-forge -c anaconda python caiman pyimagej openjdk=8
pip install --force --no-cache --pre --extra-index-url https://marcelotduarte.github.io/packages/ cx_Freeze

tzdata will be a dependent library from those above.

CanYing0913 commented 1 month ago

@marcelotduarte I have fixed the issue. However, I think the cause of issue is worth noting. I replaced all match... case... clauses with ifs and elifs and that solved it. It is weird because the python environment I used to freeze my program supports match/case statement (Python>=3.9).