ImportError:无法导入名称X
我有四个不同的文件,分别命名为:main,vector,entity和physics。我不会发布所有代码,而只会发布导入代码,因为我认为这就是错误所在。 (如果需要,我可以发布更多信息)
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, I can post more)
主要:
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运行,出现以下错误:
I then run from main.py and I get the following error:
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 ++已有很长时间了。我猜测该错误是由于两次导入实体引起的,一次是在实体中,一次是在物理中,但是我不知道解决方法。谁能帮忙?
I am very new to Python but have worked with C++ for a long time. I'm guessing that the error is due to importing entity twice, once in main, and later in physics, but I don't know a workaround. Can anyone help?
您有循环依赖的导入。 physics.py
是在 Ent
类之前从实体
导入的定义,物理
尝试导入已经初始化的实体
。从实体
模块中删除对物理学
的依赖。
You have circular dependent imports. physics.py
is imported from entity
before class Ent
is defined and physics
tries to import entity
that is already initializing. Remove the dependency to physics
from entity
module.