ImportError:没有名为books.models的模块

时间:2013-03-08 14:22:03

标签: python django

我正在通过虔诚地阅读this book来学习Django。我在第5章:模型,一切正常(在InteractiveConsole中),直到我到达Basic Data Access

epqrs@epqrs:~/webapps/djcode/mysite$ pwd
/home/epqrs/webapps/djcode/mysite
epqrs@epqrs:~/webapps/djcode/mysite$ python ../manage.py shell
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from books.models import Publisher
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named books.models
>>> import sys
>>> sys.path
['/home/epqrs/webapps/djcode', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
>>> 

我不知道为什么我会得到 ImportError:没有名为books.models的模块
books 目录位于当前目录 mysite (/ home / epqrs / webapps / djcode / mysite / books)下。

2 个答案:

答案 0 :(得分:13)

我必须将其更改为:

from books.models import Publisher, Author, Book

所以基本上,我不得不把mysite部分拿走。 另外,将已安装的应用从'mysite.books'更改为'books'

我希望它有所帮助

答案 1 :(得分:1)

我不得不做

from mysite.books.models import Publisher

让它发挥作用。