ImportError: Cannot import name X

前端 未结 16 1196
隐瞒了意图╮
隐瞒了意图╮ 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)

提交回复
热议问题