Skip to content

Commit 59de640

Browse files
committed
fix: Add runtime hook and include widgets directory in spec file
- Add runtime_hook.py to handle module import at runtime - Include entire widgets directory in spec file datas - Add comprehensive fallback import logic in __init__.py - Ensures telegram_selector module is available even if PyInstaller misses it
1 parent 8fceb5c commit 59de640

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

app/gui/widgets/__init__.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,30 @@
33
"""
44

55
# Import telegram_selector first using absolute import to ensure PyInstaller includes it
6+
# Try multiple import strategies to ensure PyInstaller detects it
67
try:
78
from app.gui.widgets.telegram_selector import TelegramSelectorDialog
89
except ImportError:
9-
# Fallback for relative import if absolute fails
10-
from .telegram_selector import TelegramSelectorDialog
10+
try:
11+
# Try relative import
12+
from .telegram_selector import TelegramSelectorDialog
13+
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)
22+
try:
23+
telegram_selector_module = importlib.import_module('telegram_selector')
24+
TelegramSelectorDialog = telegram_selector_module.TelegramSelectorDialog
25+
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")
1130

1231
from .account_widget import AccountWidget, AccountListWidget
1332
from .campaign_widget import CampaignWidget, CampaignListWidget

runtime_hook.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Runtime hook to ensure telegram_selector module is available.
3+
This is executed before the main script runs.
4+
"""
5+
6+
import sys
7+
import os
8+
9+
# Add the app directory to path if not already there
10+
if hasattr(sys, '_MEIPASS'):
11+
# We're running from a PyInstaller bundle
12+
app_path = os.path.join(sys._MEIPASS, 'app')
13+
if app_path not in sys.path:
14+
sys.path.insert(0, app_path)
15+
16+
# Try to import the module explicitly
17+
try:
18+
import app.gui.widgets.telegram_selector
19+
except ImportError:
20+
# If import fails, try to add the widgets directory
21+
widgets_path = os.path.join(sys._MEIPASS, 'app', 'gui', 'widgets')
22+
if widgets_path not in sys.path:
23+
sys.path.insert(0, widgets_path)
24+
try:
25+
import telegram_selector
26+
# Make it available as app.gui.widgets.telegram_selector
27+
import app.gui.widgets as widgets_module
28+
widgets_module.telegram_selector = telegram_selector
29+
except ImportError:
30+
pass # Will fail at runtime if really needed
31+

0 commit comments

Comments
 (0)