Skip to content

fix: resolve circular import by renaming math.py #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Kitgooo
Copy link

@Kitgooo Kitgooo commented Apr 7, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant