Google App Engine just gave me an error I don’t understand. Given a module “X” that contains the file “Car.py” which contains a class “Car”,
and given this block of code:
import X
class Passenger(db.Model):
car = db.ReferenceProperty(X.Car.Car)
I get the error:
AttributeError: 'module' object has no attribute 'Car'
But if I change it to:
from X import Car
class Passenger(db.Model):
car = db.ReferenceProperty(Car.Car)
It works. They look the same to me, but they’re clearly not. What’s the difference?
As Lattyware points out,
Xis a package, and that’s just the way packages work. Importing the outer level doesn’t automatically give you access to the modules within it. You could doimport X.Carif you wanted to refer to the whole thing asX.Car.Car.(Also please note Python is not Java: there’s no reason to have each class in a separate file, and even if you do then modules and packages usually have lower case names.)