ImportError: cannot import name X
本帖最后由 强人锁男 于 2018-9-18 16:04 编辑我有四个不同的文件:main,vector,entity和physics。)
main:
import time
from entity import Ent
from vector import Vect
#the rest just creates an entity and prints the result of movement
entity:
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
vector:
from math import *
class Vect:
#holds i, j, k, and does vector math
physics:
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 ++很长一段时间了。我猜这个错误是由于导入entity两次,一次是在main中,另一次在physics中,,但我不知道如何解决。有人可以帮忙吗? 这是循环依赖导入。physics.py从定义entity类之前导入Ent并physics尝试导入entity已经初始化的类。physics从entity模块中删除依赖项。 细节知识点,很不错 进来瞅瞅
页:
[1]