Skip to content

Cycle not detected with empty __init__.py #25

Description

@shobsi

Have a circular import

(venv) $ tree .
.
├── alpha.py
└── beta.py

1 directory, 2 files

(venv)  $ cat alpha.py
from beta import Class2
class Class1:
    obj = Class2()

(venv)  $ cat beta.py
from alpha import Class1
class Class2:
    obj = Class1()

Python fails with the ImportError as expected:

(venv)  $ python alpha.py
Traceback (most recent call last):
  File "/cur/dir/alpha.py", line 1, in <module>
    from beta import Class2
  File "/cur/dir/beta.py", line 1, in <module>
    from alpha import Class1
  File "/cur/dir/alpha.py", line 1, in <module>
    from beta import Class2
ImportError: cannot import name 'Class2' from partially initialized module 'beta' (most likely due to a circular import) (/cur/dir/beta.py)

pycycle detects the cycle as expected:

(venv) $ pycycle --here
Project successfully transformed to AST, checking imports for cycles..
Cycle Found :(
alpha -> beta: Line 1 =>> alpha
Finished.

Create an empty __init__.py in the directory. After that pycycle can no longer detect the cycle.

(venv) $ touch __init__.py
(venv) $ pycycle --here
Project successfully transformed to AST, checking imports for cycles..
No worries, no cycles here!
If you think some cycle was missed, please open an Issue on Github.
Finished. 

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions