Skip to content

Conversation

jxz21004
Copy link

@jxz21004 jxz21004 commented Sep 9, 2025

fix: resolve circular import when package renamed to other names (like "pygpc_new")

When renaming the package from "pygpc" to "pygpc_new" on the HPC, importing pygpc_new resulted in:

ImportError: cannot import name 'Beta' from partially initialized module
'pygpc_new.RandomParameter' (most likely due to a circular import)

The circular import chain was:

__init__.py → Problem.py → RandomParameter.py → BasisFunction.py
→ Grid.py → RandomParameter.py (Beta)

After investigation, BasisFunction.py did not actually use any class or function from Grid.py. Therefore, I suspect that the import of Grid in BasisFunction.py is unnecessary and the import has been removed. This breaks the circular dependency and allows successful import of pygpc_new.

It should be noted that in the original pygpc, the circular dependency also existed but happened to pass silently.
After renaming to other names like pygpc_new, the underlying issue was exposed and caused the failure.

fix: resolve circular import when package renamed to other names (like "pygpc_new")

When renaming the package from "pygpc" to "pygpc_new" on the HPC, importing
`pygpc_new` resulted in:

    ImportError: cannot import name 'Beta' from partially initialized module
    'pygpc_new.RandomParameter' (most likely due to a circular import)

The circular import chain was:

    __init__.py → Problem.py → RandomParameter.py → BasisFunction.py
    → Grid.py → RandomParameter.py (Beta)

After investigation, `BasisFunction.py` did not actually use any class or
function from `Grid.py`. Therefore, the import of `Grid` in
`BasisFunction.py` was unnecessary and has been removed. This breaks the
circular dependency and allows successful import of `pygpc_new`.

Note from Chatgpt:
- In the original `pygpc`, the same circular dependency existed, but Python's
  import order and `sys.modules` cache happened to resolve it silently.
- Renaming the package changed the module keys in `sys.modules` and exposed
  the underlying circular import that previously "passed by accident".
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