flux
flux copied to clipboard
fix: resolve circular import by renaming math.py
Issue Description
Problem: When executing python -m flux.cli --prompt="A beautiful forest", the following error occurs:
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:
-
Direct Cause:
•torch/__init__.pyattempts to import the built-inmathmodule during initialization.
• The project’s customflux/math.pysimultaneously tries to importTensorfromtorch, creating a circular dependency. -
Root Cause:
• Naming conflict:flux/math.pyshadows Python’s built-inmathmodule. Whentorchimportsmath, Python incorrectly loadsflux/math.pyinstead of the standard library.
Solution
- Rename
math.pytoflux_math.pyto avoid naming collisions. - 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).