bndr / pipreqs

pipreqs - Generate pip requirements.txt file based on imports of any project. Looking for maintainers to move this project forward.
Apache License 2.0
6.38k stars 388 forks source link

modules candidates should have __init__.py file #290

Open scouras opened 2 years ago

scouras commented 2 years ago

pipreqs was failing to include the root Django package, depending on what subdirectory I ran it over. This was due to the existence of a logs/django directory. I added a check for __init__.py and the total packages found jump by 8, up to 76. It turned out that node_modules directory was obscuring a bunch more (I should have --ignored it, of course).

The fix was a simple change to get_all_imports():

    for root, dirs, files in walk:
        dirs[:] = [d for d in dirs if d not in ignore_dirs]

        candidate = os.path.basename(root)
        initfile = os.path.join(candidate, '__init__.py')
        if os.path.exists(initfile):
            candidates.append(candidate)

I'm not sure if this is a universal improvement, so didn't make a PR for it yet. Certainly, more thought needs to be given to submodules and, more broadly, relative imports.