Nonocycle – Prevent circular imports in Python

2 pointsposted 12 hours ago
by nonocycle

1 Comments

nonocycle

12 hours ago

Nonocycle is a small tool I developed to detect potential circular imports before they are introduced into a codebase. It achieves this by enforcing a strict separation of concerns between sub-packages.

In my experience, circular imports are often not introduced in a single PR but build up over time. Developer A adds a dependency between modules X and Y. Developer B adds a dependency between Y and Z. Developer C wants to add more code, encounters a circular import error, and ends up in refactoring hell. This is especially problematic in larger projects where not all contributors are aware of how different modules depend on each other.

Most discussions about circular imports focus on how to fix them, while much less attention is paid to prevention. The common advice is to "split code into well-separated modules," which is of course correct but high-level and difficult to enforce. I created nonocycle because I couldn't find another tool that ensures modules remain well separated.

> This is a weekend project and is by no means production-ready. The heavy lifting is done by the excellent pydeps and networkx libraries. Please let me know if there are any other tools in the space :)