django 1.4,python 2.6.6.
I have an app “jobs”.
my files tree:
djproject/
|-- djproject
| |-- db
| | `-- tdata.db
| |-- __init__.py
| |-- __init__.pyc
| |-- jobs
| | |-- admin.py
| | |-- admin.pyc
| | |-- __init__.py
| | |-- __init__.pyc
| | |-- models.py
| | |-- models.pyc
| | |-- tests.py
| | `-- views.py
| |-- settings.py
| |-- settings.pyc
| |-- urls.py
| |-- urls.pyc
| |-- wsgi.py
| `-- wsgi.pyc
`-- manage.py
my manage.py file:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djproject.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Then I run “python manage.py shell” in which is the same level folder with file “manage.py”.
And then I type “from jobs.models import Job”.
Errors as follow(relative path):
>>> from jobs.models import Job
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named jobs.models
but, If I use full path
>>> from djproject.jobs.models import Job
error disapear, why?
This is as expected because you’re now running
manage.pyfrom a different directory than in previous versions of Django. See Updated default project layout and manage.py in the Django documentation for more details.You’re now starting
manage.pyfrom a higher directory in the tree so you’ll need to includedjprojectwhen importing the code. The documentation also gives a suggestion on how to do it differently (if you want):