Skip to content

Commit 1e97f9f

Browse files
committed
fix: Use lazy import pattern for TelegramSelectorDialog to help PyInstaller
1 parent 59de640 commit 1e97f9f

1 file changed

Lines changed: 31 additions & 21 deletions

File tree

app/gui/widgets/__init__.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,41 @@
22
Reusable GUI widgets for the application.
33
"""
44

5-
# Import telegram_selector first using absolute import to ensure PyInstaller includes it
6-
# Try multiple import strategies to ensure PyInstaller detects it
7-
try:
8-
from app.gui.widgets.telegram_selector import TelegramSelectorDialog
9-
except ImportError:
5+
# Lazy import function for TelegramSelectorDialog to avoid import errors at module level
6+
# This allows PyInstaller to include the module even if it's not directly imported
7+
def _get_telegram_selector_dialog():
8+
"""Lazy import function for TelegramSelectorDialog."""
109
try:
11-
# Try relative import
12-
from .telegram_selector import TelegramSelectorDialog
10+
from app.gui.widgets.telegram_selector import TelegramSelectorDialog
11+
return TelegramSelectorDialog
1312
except ImportError:
14-
# Last resort: try importing the module directly
15-
import importlib
16-
import sys
17-
import os
18-
# Get the directory of this file
19-
widgets_dir = os.path.dirname(os.path.abspath(__file__))
20-
if widgets_dir not in sys.path:
21-
sys.path.insert(0, widgets_dir)
2213
try:
23-
telegram_selector_module = importlib.import_module('telegram_selector')
24-
TelegramSelectorDialog = telegram_selector_module.TelegramSelectorDialog
14+
from .telegram_selector import TelegramSelectorDialog
15+
return TelegramSelectorDialog
2516
except ImportError:
26-
# If all else fails, create a dummy class to prevent complete failure
27-
class TelegramSelectorDialog:
28-
def __init__(self, *args, **kwargs):
29-
raise RuntimeError("TelegramSelectorDialog module not found. Please rebuild the executable with PyInstaller using main.spec")
17+
import importlib
18+
import sys
19+
import os
20+
# Get the directory of this file
21+
widgets_dir = os.path.dirname(os.path.abspath(__file__))
22+
if widgets_dir not in sys.path:
23+
sys.path.insert(0, widgets_dir)
24+
try:
25+
telegram_selector_module = importlib.import_module('telegram_selector')
26+
return telegram_selector_module.TelegramSelectorDialog
27+
except ImportError:
28+
# If all else fails, create a dummy class
29+
class TelegramSelectorDialog:
30+
def __init__(self, *args, **kwargs):
31+
raise RuntimeError("TelegramSelectorDialog module not found. Please rebuild the executable with PyInstaller using main.spec")
32+
return TelegramSelectorDialog
33+
34+
# Import it immediately to ensure PyInstaller sees it, but use the function for lazy loading
35+
try:
36+
from app.gui.widgets.telegram_selector import TelegramSelectorDialog
37+
except ImportError:
38+
# If import fails, use lazy loader
39+
TelegramSelectorDialog = _get_telegram_selector_dialog()
3040

3141
from .account_widget import AccountWidget, AccountListWidget
3242
from .campaign_widget import CampaignWidget, CampaignListWidget

0 commit comments

Comments
 (0)