मेरे पास नाम की चार अलग-अलग फाइलें हैं: मुख्य, वेक्टर, इकाई और भौतिकी। मैं सभी कोड पोस्ट नहीं करूंगा, सिर्फ आयात, क्योंकि मुझे लगता है कि जहां त्रुटि है। (आप चाहें तो मैं और पोस्ट कर सकते हैं)
मुख्य:
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
मैं पायथन के लिए बहुत नया हूं लेकिन लंबे समय तक सी ++ के साथ काम किया है। मैं अनुमान लगा रहा हूं कि त्रुटि दो बार इकाई आयात करने के कारण है, एक बार मुख्य रूप से, और बाद में भौतिकी में, लेकिन मुझे कोई हल नहीं पता। क्या कोई मदद कर सकता है?
from <module> import <name>
, या from <modlue> import *
। मॉड्यूल नाम स्थान के तहत आयात करने के लिए बेहतर है कि नामांकित संदर्भों को अधिलेखित करने की संभावना को रोकने के लिए।
Entity
और Vector
के बजाय Ent
और Vect
, इस तरह के नाम छोटे कर लिए कोई कारण नहीं है। और हाँ, उपयोग करें import vector
और फिर x = vector.Vector(0,0,0)
।