1# While you should definitely avoid circular dependencies,
2# you can defer imports in python.
3# for example:
4
5import SomeModule
6
7def someFunction(arg):
8 from some.dependency import DependentClass
9
10#this ( at least in some instances ) will circumvent the error.
1if you encounter this error:
2
3ImportError: Cannot import name whatever
4
5with a python file called "whatever",
6then this may be cause by the following issues:
7 1. file "whatever" is not in the same folder as the current file.
8 this means that you need to place file "whatever" inside
9 the same file that gave you the import error.
10 2. library/module "whatever" is not installed.
11 if "whatever" is a third-party library/module,
12 then you need to install the library/module.
13 This is usually done with "pip install whatever",
14 but exceptions do exist that the command is not
15 the proper command to install "whatever"
16 3. if this file is in another folder, but you don't want to move it.
17 in this case, you should add this at the top of your file:
18 import sys
19 sys.path.append('path/to/file/whatever.py')
20 replace 'path/to/file' with the proper directory of "whatever.py"
21 4. you forgot to create "whatever.py"!
22 well... just remember to do that before importing.
23
24This does not include all of the possibilities. Hope this helped :D
1import SomeModule
2
3def someFunction(arg):
4 from some.dependency import DependentClass
1While you should definitely avoid circular dependencies, you can defer imports in python.
2
3for example:
4
5import SomeModule
6
7def someFunction(arg):
8 from some.dependency import DependentClass
9this ( at least in some instances ) will circumvent the error.
1This is a circular dependency. It can be solved without any structural modifications to the code. The problem occurs because in vector you demand that entity be made available for use immediately, and vice versa. The reason for this problem is that you asking to access the contents of the module before it is ready -- by using from x import y. This is essentially the same as
2
3import x
4y = x.y
5del x
6Python is able to detect circular dependencies and prevent the infinite loop of imports. Essentially all that happens is that an empty placeholder is created for the module (ie. it has no content). Once the circularly dependent modules are compiled it updates the imported module. This is works something like this.
7
8a = module() # import a
9
10# rest of module
11
12a.update_contents(real_a)
13For python to be able to work with circular dependencies you must use import x style only.
14
15import x
16class cls:
17 def __init__(self):
18 self.y = x.y
19Since you are no longer referring to the contents of the module at the top level, python can compile the module without actually having to access the contents of the circular dependency. By top level I mean lines that will be executed during compilation as opposed to the contents of functions (eg. y = x.y). Static or class variables accessing the module contents will also cause problems.
1Search your entire project/solution (generally ctrl-shift-f) for 'flask' or whatever the name import error is.
2You may have it being imported twice and just need to remove one.