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
Change Name of working python script: Changing the name of the Working file different from the module which is imported in the script can avoid the Circular Imports problem. Import the module: Avoid importing objects or functions from a module that can cause Circular Imports.
In simplest terms, a circular import occurs when module A tries to import and use an object from module B, while module B tries to import and use an object from module A. You can see it on on an example with simple modules a.py and b.py: # a.py snippet. print('First line of a.py') from package.
Let's import the functions particularly instead of the whole file. To avoid circular import errors, you can move ```from module import X``` to a place it is needed.
A circular dependency occurs when two or more modules depend on each other. This is due to the fact that each module is defined in terms of the other (See Figure 1). For example: functionA(): functionB() And functionB(): functionA() The code above depicts a fairly obvious circular dependency.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With