From 7755fdfe06bf4994a93fe86c4e9f9d1ca3e5d3ab Mon Sep 17 00:00:00 2001 From: Amir Rossert Date: Mon, 18 Mar 2019 23:35:16 +0200 Subject: [PATCH] when using PyInstaller, list modules directly from files instead of using pkgutil.iter_modules which is not suported --- faker/utils/loading.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/faker/utils/loading.py b/faker/utils/loading.py index 8a56b1f6bf..2e6f46d17c 100644 --- a/faker/utils/loading.py +++ b/faker/utils/loading.py @@ -7,8 +7,15 @@ def get_path(module): if getattr(sys, 'frozen', False): # frozen - base_dir = os.path.dirname(sys.executable) - lib_dir = os.path.join(base_dir, "lib") + + if getattr(sys, '_MEIPASS', False): + # PyInstaller + lib_dir = getattr(sys, '_MEIPASS') + else: + # others + base_dir = os.path.dirname(sys.executable) + lib_dir = os.path.join(base_dir, "lib") + module_to_rel_path = os.path.join(*module.__package__.split(".")) path = os.path.join(lib_dir, module_to_rel_path) else: @@ -19,9 +26,14 @@ def get_path(module): def list_module(module): path = get_path(module) - modules = [name for _, name, - is_pkg in pkgutil.iter_modules([path]) if is_pkg] - return modules + + if getattr(sys, '_MEIPASS', False): + # PyInstaller + return [name for name in os.listdir(path) + if os.path.isdir(os.path.join(path, name)) and + "__init__.py" in os.listdir(os.path.join(path, name))] + else: + return [name for _, name, is_pkg in pkgutil.iter_modules([path]) if is_pkg] def find_available_locales(providers):