Skip to main content

Circular Import Detector

A Python tool to detect circular imports in your Python projects. This tool analyzes import statements statically and builds a dependency graph to identify circular import cycles that would only be discovered at runtime.

Features

  • 🔍 Static Analysis: Detects circular imports without running your code
  • 🎯 Pre-commit Integration: Prevents circular imports from being committed
  • 📊 Detailed Reporting: Shows exactly which files and modules are involved in cycles
  • 🏗️ Package Support: Handles complex package structures and relative imports
  • ⚡ Fast: Efficiently processes large codebases
  • 🛡️ Safe: Only analyzes internal project imports, ignores external dependencies

Installation

From Source

git clone <repository-url>
cd circular-import-detector
pip install -e .

Using pip (when published)

pip install circular-import-detector

Usage

Command Line Interface

Basic Usage

# Check current directory
circular-import-detector

# Check specific directory
circular-import-detector /path/to/your/project

# Quiet mode (only show output if circular imports found)
circular-import-detector --quiet

# Exit with error code if circular imports found (useful for CI/CD)
circular-import-detector --exit-code

Example Output

Found 1 circular import cycle(s):

Cycle 1:
  module_a -> module_b
  module_b -> module_c
  module_c -> module_a (circular)
  Files involved:
    module_a: /path/to/project/module_a.py
    module_b: /path/to/project/module_b.py
    module_c: /path/to/project/module_c.py

Pre-commit Hook Integration

The tool is designed to work seamlessly with pre-commit to prevent circular imports from being committed to your repository.

Setup

  1. Install pre-commit (if you haven't already):

    pip install pre-commit
    
  2. Add to your .pre-commit-config.yaml:

-   repo: local
    hooks:
      - id: circular-imports
        name: Check for circular imports (changed files)
        entry: circular-import-precommit
        language: python
        additional_dependencies:
          - circular-import-detector==1.0.18
        pass_filenames: true
        types_or: [python]
        stages: [pre-commit]
  1. Install the hooks:
    pre-commit install
    

Now, every time you commit Python files, the tool will check for circular imports and block the commit if any are found.

Programmatic Usage

You can also use the detector in your Python code:

from circular_import_detector import CircularImportDetector

# Initialize detector
detector = CircularImportDetector('/path/to/your/project')

# Detect circular imports
has_cycles, cycles = detector.detect_circular_imports()

if has_cycles:
    print(f"Found {len(cycles)} circular import cycles!")
    for cycle in cycles:
        print("Cycle:", " -> ".join(cycle))
else:
    print("No circular imports detected.")

How It Works

The tool performs static analysis of your Python code by:

  1. Parsing Python files using the ast module to extract import statements
  2. Building a dependency graph of modules and their imports
  3. Filtering internal imports to focus only on your project's modules
  4. Detecting cycles using depth-first search algorithm
  5. Reporting results with detailed information about each cycle

Supported Import Types

  • import module
  • from module import something
  • from package.submodule import something
  • from . import module (relative imports)
  • from ..parent import module (relative imports)

What Gets Analyzed

  • ✅ All .py files in your project
  • ✅ Package structures with __init__.py
  • ✅ Nested packages and submodules
  • ✅ Relative imports within packages
  • ❌ External dependencies (ignored)
  • ❌ Dynamic imports (not detectable statically)

Configuration

The tool works out of the box with sensible defaults, but you can customize its behavior:

Project Structure

The detector automatically:

  • Skips common non-source directories (.git, __pycache__, build, dist, etc.)
  • Handles package structures correctly
  • Converts file paths to proper module names

Common Circular Import Patterns

Simple Cycle

# file_a.py
import file_b

# file_b.py
import file_a  # Creates cycle: file_a -> file_b -> file_a

Complex Cycle

# models.py
from views import get_context

# views.py
from utils import helper

# utils.py
from models import MyModel  # Creates cycle: models -> views -> utils -> models

Package Cycle

# package_a/__init__.py
from package_b import something

# package_b/__init__.py
from package_a import something_else  # Creates cycle

Troubleshooting

Common Issues

  1. "No Python files found"

    • Ensure you're running the tool from the correct directory
    • Check that your Python files have .py extension
  2. Relative imports not detected correctly

    • Ensure your package structure has proper __init__.py files
    • The tool analyzes from the project root you specify
  3. External imports being flagged

    • This shouldn't happen, but if it does, the filtering logic may need adjustment
    • Open an issue with details about your project structure

Performance Tips

  • The tool is designed to handle large codebases efficiently
  • For very large projects, consider running it on specific subdirectories
  • Use --quiet mode in automated environments to reduce output

Development

Running Tests

python -m pytest test_circular_imports.py -v

Running the Tool on Itself

# Test the tool on its own codebase
python circular_import_detector.py .

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for your changes
  4. Ensure all tests pass
  5. Submit a pull request

License

MIT License - see LICENSE file for details.

Metadata

Release files for circular-import-detector 1.0.18

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for circular-import-detector 1.0.18
File Size Uploaded Provenance Actions
circular_import_detector-1.0.18.tar.gz 17.0 kB Without provenance Details for circular_import_detector-1.0.18.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for circular-import-detector 1.0.18
File Interpreter ABI Platform Actions
circular_import_detector-1.0.18-py3-none-any.whl Python 3 none any Details for circular_import_detector-1.0.18-py3-none-any.whl

Total release size: 34.1 kB

Release files / circular_import_detector-1.0.18.tar.gz

Download URL circular_import_detector-1.0.18.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b6ecbcd378799d31ee9edc0f5356ea97d751e19e7fdead6c13cfd4b145a6c299
BLAKE2b-256 checksum
How to use checksums
6bbd5f8fe59cd54986138869e750d491856a7166e18e4a69a6a8b37a5eb09bbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Release files / circular_import_detector-1.0.18-py3-none-any.whl

Download URL circular_import_detector-1.0.18-py3-none-any.whl
Size 17.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b25c64e493a2808d380150bb1fd8e2e5771a9baea3cb3cb61eabda37e06dca7a
BLAKE2b-256 checksum
How to use checksums
9bb65cca9553b72a14ef49a224a20eb614054bd00217a13e022dcc889416b499
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Security for circular-import-detector 1.0.18

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page