Skip to content

Commit

Permalink
when using PyInstaller, list modules directly from files instead of u…
Browse files Browse the repository at this point in the history
…sing pkgutil.iter_modules which is not suported (#927)
  • Loading branch information
arossert authored and fcurella committed Mar 19, 2019
1 parent 6db5503 commit eb7d9c8
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions faker/utils/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down

0 comments on commit eb7d9c8

Please sign in to comment.