From 107348f7eca1eff3f47792f21ef78887ffa3ff08 Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Thu, 15 Feb 2024 17:54:10 +0100 Subject: [PATCH] Allow access to data files when running in a frozen environment (eg. PyInstaller executable). #152 --- codicefiscale/data.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/codicefiscale/data.py b/codicefiscale/data.py index fc90115..1436093 100644 --- a/codicefiscale/data.py +++ b/codicefiscale/data.py @@ -1,13 +1,23 @@ from __future__ import annotations - +import os +import sys from datetime import datetime from typing import Any import fsutil +def get_data_basedir() -> str: + if getattr(sys, "frozen", False): + # standalone executable (eg. PyInstaller) + return os.path.dirname(sys.executable) + return __file__ + + def get_data(filename: str) -> Any: - return fsutil.read_file_json(fsutil.join_path(__file__, f"data/{filename}")) + return fsutil.read_file_json( + fsutil.join_path(get_data_basedir(), f"data/{filename}") + ) def get_municipalities_data() -> Any: