Pyinstaller: executable only runs when `console=True`

I’m trying to compile a python application using Pyinstaller with the following spec file:

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


a = Analysis(
    ['src\cdi_live.py'],
    pathex=['src', 'venv/Lib/site-packages'],
    binaries=[],
    datas=[
        ('example_data/*.tif', 'example_data'),
        ('LICENSE.txt', '.'),
        ('README.md', '.')
    ],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='cdi_live',
    debug="all",
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='cdi_live',
)

When I run Pyinstaller on this, it works exactly as expected. No red flags while compiling, and the output goes into my dist directory. When I run the executable, a terminal opens up, I get my verbose debugging messages, and then my GUI opens on top of that. Everything in the application functions perfectly, and I breathe a sigh of relief.

Then, because I don’t want to open a console every time I run my application, I change one line in the spec file:

exe = EXE(
   ...
   console=False,
   ...
)

I run Pyinstaller again. At first, it seems to have worked again. But when I run the executable, I get a message saying that it failed to execute the script due to an unhandled AttributeError when I imported scipy.ndimage (full traceback given below).

Why would that single line cause the compiled executable to fail, and how do I fix it?

Additional information

I don’t know how much of this will be useful, but I’ll include everything I think might have bearing on the question.

I’ve gone back and forth with the console option and it always works with console=True and always fails with console=False.

I’ve used the same spec file to compile this app in the past, and it worked fine. Since the previous version, I’ve added some matplotlib.backends.backend_tkagg.FigureCanvasTkAgg widgets.

The traceback that shows up in the error pop-up is long and burrows deep into the dependencies:

Traceback (most recent call last):
  File "cdi_live.py", line 20, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "phasing.py", line 7, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "scipyndimage__init__.py", line 152, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "scipyndimage_filters.py", line 37, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "scipy_lib_util.py", line 18, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "scipy_lib_array_api.py", line 21, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "scipy_libarray_api_compatnumpy__init__.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "numpyf2py__init__.py", line 19, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "numpyf2pyf2py2e.py", line 23, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "numpyf2pycrackfortran.py", line 159, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "numpyf2pyauxfuncs.py", line 19, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstallerloaderpyimod02_importers.py", line 378, in exec_module
  File "numpyf2pycfuncs.py", line 19, in <module>
AttributeError: 'NoneType' object has no attribute 'write'

The last call to my own code (File "phasing.py", line 7, in <module>) is an attempt to import scipy.ndimage. Before that point, python has already imported tkinter, numpy, and several packages from matplotlib.

Running the EXE with a debugger active doesn’t provide much help. The output is identical to the console output. After I close out the error message, I get a few additional lines:

00000047    33.55646133 [31580] [PYI-31580:DEBUG] LOADER: ERROR.    
00000048    33.55653381 [31580] [PYI-31580:DEBUG] LOADER: cleaning up Python interpreter... 
00000049    33.63221741 [31580] [PYI-31580:DEBUG] LOADER: unloading Python shared library...    
00000050    33.63228989 [31580] [PYI-31580:DEBUG] LOADER: end of process reached!

2

that’s a bug in numpy 2.0 that was fixed 2 months ago, https://github.com/numpy/numpy/issues/26862

either upgrade to the latest numpy 2.1 or downgrade to 1.26, or just dump stderr manually by putting this as the first line in your program.

import sys
class dummyOut:
    def write(self, data):
        pass
sys.stderr = dummyOut()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật