ImportError: Cannot import name X

前端 未结 16 1518
隐瞒了意图╮
隐瞒了意图╮ 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 05:10

    If you are importing file1.py from file2.py and used this:

    if __name__ == '__main__':
        # etc
    

    Variables below that in file1.py cannot be imported to file2.py because __name__ does not equal __main__!

    If you want to import something from file1.py to file2.py, you need to use this in file1.py:

    if __name__ == 'file1':
        # etc
    

    In case of doubt, make an assert statement to determine if __name__=='__main__'

提交回复
热议问题