I’m a django newbie. I just installed v 1.3.1 on windows vista (using setup.py install) for python 2.5
When I start a python shell and try to import django.db I get the following circular import error
>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
>>> import django.db
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\lib\site-packages\django\db\__init__.py", line 78, in <module>
connection = connections[DEFAULT_DB_ALIAS]
File "C:\Python25\lib\site-packages\django\db\utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Python25\lib\site-packages\django\db\utils.py", line 33, in load_backend
return import_module('.base', backend_name)
File "C:\Python25\lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
File "C:\Python25\Lib\site-packages\django\db\backends\sqlite3\base.py", line 14, in <module>
from django.db import utils
ImportError: cannot import name utils
>>>
Looking at the code, I can see that django\db\backends\sqlite3\base.py imports django\db\utils.py, but then this file also imports base.py (using import_module). Isn’t that necessarily going to crash due to circular import?
On the other hand, if I use the shell from python manage.py shell everything works fine, so there must be something I can run on my plain shell to make it work
Thanks for any hints!
EDIT:
Delyan came up with two possible solutions:
C:\Users\xulo>cd c:\django_example
c:\django_example>cd mysite
c:\django_example\mysite>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
>>> import sys
>>> sys.path.append('c:\\django_example\\mysite')
>>> sys.path.append('c:\\django_example')
>>> from django import db
>>>
or
c:\django_example\mysite>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import settings
>>> import django.core.management
>>> django.core.management.setup_environ(settings)
'c:\\django_example\\mysite'
>>> from django import db
>>>
Both work well, but I’ll leave the question open for now to see if someone has a simple explanation of why, and why that sorts the apparent circular import between utils.py and base.py
It’s rather annoying but Django wants you to have your project’s folder and its parent in
sys.path. You can see this happening insetup_environindjango.core.management.__init__There was an issue raised recently and this might be refactored in the near future but for now just add those two folders to any custom scripts (though you should really be adding them as
manage.pycommands).Edit: This has been partially refactored in Django 1.4.