Search code examples
python-3.xrecursiontraversalos.walkscandir

List all valid/importable Python files under a directory


I have the need to get a list of every valid/importable Python file living under the current module, i.e. down the current dir recursively.

Is there a lib to do that? I've failed in finding one.

Airflow, mypy, black, pytest, unittest and etc all use some kind of recursive directory traversal to discover valid Python files but there is no standard in doing this, each lib implements this feature on their own.

In case there isn't a good library to abstract this, are there any known considerations on investing in a naive/simplistic approach of listing all .py files under directories containing an __init__.py file and excluding some dirs (e.g. .*, __pycache__, etc) based on an exclusion list?

I'm interested in Python 3.5+ only.


Solution

  • Rather than hardcoding the implementation to my own project I've published a small opensource lib to this.

    pycollect is released under MIT and is scoped to collect any valid Python files given a root directory. It includes some basic tweaking to behavior and supports exclusion patterns. Though aimed for collecting Python files only it can be easily forked to become a more generic files/directories collector.

    As of the time of writing I'm the official maintainer.