1# Python ImportError: cannot import name error
2# is cause by either:
3# 1: The import module/class is inaccessible (not installed or ot reacheable by current PYTHONPATH)
4# Fix : Install module with pip or easy install or correct PYTHONPATH
5
6# 2: You have created a circular dependancy such as:
7
8# in foo.py
9...
10import bar
11...
12
13# in bar.py
14...
15import foo
16...
17
18# How to Fix it
19# 1 - refactor your code (not always straitforward ...)
20# 2 - Of course you should definitely avoid circular dependencies,
21# but sometimes as a quick fix you can use some kind of lazy loading to defer imports
22
23# In a method or function
24
25def function_using_foo():
26 import foo
27 # now u can use foo here
28 foo.baz()
29 ...
30
31def function_returning_foo():
32 import foo
33 # Hint: You can cache foo for more efficiency ...
34 return foo
35
36# now u can use foo everywhere this way
37function_returning_foo().baz()
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.
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.
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