ImportError: Cannot import name X

前端 未结 16 1198
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 04:33

I have four different files named: main, vector, entity and physics. I will not post all the code, just the imports, because I think that\'s where the error is. (If you want

相关标签:
16条回答
  • 2020-11-22 04:47

    As already mentioned, this is caused by a circular dependency. What has not been mentioned is that when you're using Python typing module and you import a class only to be used to annotate Types, you can use Forward references:

    When a type hint contains names that have not been defined yet, that definition may be expressed as a string literal, to be resolved later.

    and remove the dependency (the import), e.g. instead of

    from my_module import Tree
    
    def func(arg: Tree):
        # code
    

    do:

    def func(arg: 'Tree'):
        # code
    

    (note the removed import statement)

    0 讨论(0)
  • 2020-11-22 04:47

    The problem is clear: circular dependency between names in entity and physics modules.

    Regardless of importing the whole module or just a class, the names must be loaded .

    Watch this example:

    # a.py
    import b
    def foo():
      pass
    b.bar()
    
    # b.py
    import a
    def bar():
      pass
    a.foo()
    

    This will be compiled into:

    # a.py
    # import b
    # b.py
    # import a # ignored, already importing
    def bar():
      pass
    a.foo()
    # name a.foo is not defined!!!
    # import b done!
    def foo():
      pass
    b.bar()
    # done!
    

    With one slight change we can solve this:

    # a.py
    def foo():
      pass
    import b
    b.bar()
    
    # b.py
    def bar():
      pass
    import a
    a.foo()
    

    This will be compiled into:

    # a.py
    def foo():
      pass
    # import b
    # b.py
    def bar():
      pass
    # import a # ignored, already importing
    a.foo()
    # import b done!
    b.bar()
    # done!
    
    0 讨论(0)
  • 2020-11-22 04:52

    To make logic clear is very important. This problem appear, because the reference become a dead loop.

    If you don't want to change the logic, you can put the some import statement which caused ImportError to the other position of file, for example the end.

    a.py

    from test.b import b2
    
    def a1():
        print('a1')
        b2()
    

    b.py

    from test.a import a1
    
    def b1():
        print('b1')
        a1()
    
    def b2():
        print('b2')
    
    if __name__ == '__main__':
        b1()
    

    You will get Import Error: ImportError: cannot import name 'a1'

    But if we change the position of from test.b import b2 in A like below:

    a.py

    def a1():
        print('a1')
        b2()
    
    from test.b import b2
    

    And the we can get what we want:

    b1
    a1
    b2
    
    0 讨论(0)
  • 2020-11-22 04:53

    Don't see this one here yet - this is incredibly stupid, but make sure you're importing the correct variable/function.

    I was getting this error

    ImportError: cannot import name IMPLICIT_WAIT

    because my variable was actually IMPLICIT_TIMEOUT.

    when I changed my import to use the correct name, I no longer got the error

    0 讨论(0)
  • 2020-11-22 04:53

    Not specifically for this asker, but this same error will show if the class name in your import doesn't match the definition in the file you're importing from.

    0 讨论(0)
  • 2020-11-22 04:55

    Also not directly relevant to the OP, but failing to restart a PyCharm Python console, after adding a new object to a module, is also a great way to get a very confusing ImportError: Cannot import name ...

    The confusing part is that PyCharm will autocomplete the import in the console, but the import then fails.

    0 讨论(0)
提交回复
热议问题