6

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

xuloChavez
  • 439
  • 6
  • 13

1 Answers1

7

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 in setup_environ in django.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.py commands).

Edit: This has been partially refactored in Django 1.4.

Delyan
  • 8,881
  • 4
  • 37
  • 42
  • Thanks Delyan, it did work (either adding both folders, or running setup_environ). However I'm still not sure how that changes the problem that utils.py and base.py import each other circularly - I guess import_module is not quite the same as plain import, any chance of a quick explanation? – xuloChavez Oct 23 '11 at 14:14
  • No, it's exactly the same. If I had to guess, I'd say it boils down to how Python identifies imported packages. `import settings` is different from `import project.settings`. I don't have the time to dig through Django to figure out this particular issue, though. There's plenty of voodoo magic where Django and imports are involved.. – Delyan Oct 23 '11 at 14:27
  • ok it does work so thanks a lot, I'll leave it open for a couple of days but if no one else can explain it i'll accept yours – xuloChavez Oct 23 '11 at 14:32
  • I had a very similar problem that also turned out to be a path issue. For me, adding the path to the module from which I needed to import to the sys.path solved the problem (not the project parent, in my case). For me, I added C:\Python27\lib\site-packages\django to the path, where django.db was the module from which I was attempting to import. Thanks for the help. – Justin O Barber Dec 22 '12 at 17:59