Created
December 22, 2025 08:29
-
-
Save prerakmody/a420f5dd901c4907ccce3e49e75b35b1 to your computer and use it in GitHub Desktop.
Pyinstaller commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import subprocess | |
| import sys | |
| import site | |
| import shutil | |
| from pathlib import Path | |
| # Find the Magika models directory | |
| def find_magika_models(): | |
| try: | |
| # Try to find magika in site-packages | |
| site_packages = site.getsitepackages() | |
| for sp in site_packages: | |
| magika_path = os.path.join(sp, "magika") | |
| models_path = os.path.join(magika_path, "models") | |
| config_path = os.path.join(magika_path, "config") | |
| if os.path.exists(models_path) and os.path.exists(config_path): | |
| return models_path, config_path | |
| # If not found in site-packages, check if it's in the current environment | |
| conda_prefix = os.environ.get("CONDA_PREFIX") | |
| if conda_prefix: | |
| magika_path = os.path.join(conda_prefix, "Lib", "site-packages", "magika") | |
| models_path = os.path.join(magika_path, "models") | |
| config_path = os.path.join(magika_path, "config") | |
| if os.path.exists(models_path) and os.path.exists(config_path): | |
| return models_path, config_path | |
| return None, None | |
| except Exception as e: | |
| print(f"Error finding magika models: {e}") | |
| return None | |
| def main(): | |
| magika_models = find_magika_models() | |
| if not magika_models: | |
| print("Couldn't find magika models directory!") | |
| sys.exit(1) | |
| print(f"Found magika models at: {magika_models}") | |
| dist_path = "../insurance_renewal_app_dist/" | |
| # Build the PyInstaller command | |
| cmd = [ | |
| "pyinstaller", | |
| "--onedir", | |
| "--clean", | |
| "--noconfirm", | |
| "--name=Insurance Renewal Tracker", | |
| f"--distpath={dist_path}", | |
| "--paths=.", | |
| "--add-data=src:src", | |
| "--add-data=templates:templates", | |
| "--add-data=static:static", | |
| # "--add-data=credentials.json:.", | |
| "--add-data=.env:.", | |
| f"--add-data={magika_models[0]}:magika/models", | |
| f"--add-data={magika_models[1]}:magika/config", | |
| "--hidden-import=pystray", | |
| "--hidden-import=PIL", | |
| "--hidden-import=magika", | |
| "--hidden-import=babel.numbers", | |
| "app.py" | |
| ] | |
| # Use the "--windowed" flag to not show the terminal. | |
| # Execute the command | |
| result = subprocess.call(cmd) | |
| if result == 0: | |
| print("\nBuild successful! Compressing output folder...") | |
| # Get the full path to the dist folder | |
| dist_folder = Path(dist_path).resolve() | |
| app_folder = dist_folder / "Insurance Renewal Tracker" | |
| if app_folder.exists(): | |
| # Create a zip archive | |
| archive_name = dist_folder / "Insurance_Renewal_Tracker" | |
| print(f"Creating archive: {archive_name}.zip") | |
| shutil.make_archive(str(archive_name), 'zip', app_folder) | |
| print(f"Archive created successfully at: {archive_name}.zip") | |
| else: | |
| print(f"Warning: Output folder not found at {app_folder}") | |
| else: | |
| print(f"\nBuild failed with exit code {result}") | |
| sys.exit(result) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment