我有四个不同的文件,分别命名为:main,vector,entity和physics。我不会发布所有代码,而只会发布导入代码,因为我认为这就是错误所在。(如果需要,我可以发布更多信息)
main,vector,entity
physics
主要:
import time from entity import Ent from vector import Vect #the rest just creates an entity and prints the result of movement
实体:
from vector import Vect from physics import Physics class Ent: #holds vector information and id def tick(self, dt): #this is where physics changes the velocity and position vectors
向量:
from math import * class Vect: #holds i, j, k, and does vector math
物理:
from entity import Ent class Physics: #physics class gets an entity and does physics calculations on it.
然后,我从main.py运行,出现以下错误:
Traceback (most recent call last): File "main.py", line 2, in <module> from entity import Ent File ".../entity.py", line 5, in <module> from physics import Physics File ".../physics.py", line 2, in <module> from entity import Ent ImportError: cannot import name Ent
我对Python非常陌生,但是已经使用C ++了很长时间。我猜测该错误是由于两次导入实体引起的,一次是在主体中,一次是在物理中,但是我不知道解决方法。有人可以帮忙吗?
你有循环依赖进口。physics.py从定义entity类之前导入,Ent并physics尝试导入entity已初始化的类。physics从entity模块中删除对的依赖。
physics.py
entity
Ent