flux icon indicating copy to clipboard operation
flux copied to clipboard

fix: resolve circular import by renaming math.py

Open Kitgooo opened this issue 7 months ago • 0 comments

Issue Description

Problem: When executing python -m flux.cli --prompt="A beautiful forest", the following error occurs:

Screenshot 2025-04-07 at 22 51 52
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/jaylaw/flux/src/flux/cli.py", line 7, in <module>
    import torch
  File "/Users/jaylaw/flux/.venv/lib/python3.13/site-packages/torch/__init__.py", line 18, in <module>
    import math
  File "/Users/jaylaw/flux/src/flux/math.py", line 3, in <module>
    from torch import Tensor
ImportError: cannot import name 'Tensor' from partially initialized module 'torch' (most likely due to a circular import) (/Users/jaylaw/flux/.venv/lib/python3.13/site-packages/torch/__init__.py)

Cause:

  1. Direct Cause:
    torch/__init__.py attempts to import the built-in math module during initialization.
    • The project’s custom flux/math.py simultaneously tries to import Tensor from torch, creating a circular dependency.

  2. Root Cause:
    • Naming conflict: flux/math.py shadows Python’s built-in math module. When torch imports math, Python incorrectly loads flux/math.py instead of the standard library.

Solution

  1. Rename math.py to flux_math.py to avoid naming collisions.
  2. Update all references to the renamed file (There is only one: src/flux/models/layers.py).

Validation

• Local test: Executing python -m flux.cli --prompt="A beautiful forest" resolves the original error (new issues unrelated to this fix may arise).
Screenshot 2025-04-07 at 23 09 16

Kitgooo avatar Apr 07 '25 15:04 Kitgooo