[Django] #20704: DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"

116 views
Skip to first unread message

Django

unread,
Jul 4, 2013, 4:56:17 PM7/4/13
to django-...@googlegroups.com
#20704: DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"
----------------------------------------------+--------------------
Reporter: artscoop | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.5
Severity: Normal | Keywords: router
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
This bug occurs also on other versions of Django.
I have a Django project which usually works well.
Today I tried to introduce the use of another database. (one is
PostgreSQL, the new one is SQLite)

I've setup my settings.py as follows :

{{{#!python
DATABASE_ROUTERS = ['engine.db.routers.custom.Router',]
}}}

The path to the router is fine, because changing it to a non-existing one
raises a correct error. The router inherits from object, and defines the 4
functions shown in the Django documentation. The
{{{allow_relation}}} and {{{allow_syncdb}}} members return True (tried
with None), {{{db_for_write}}} and {{{db_for_read}}} return a database
name or None.
I have properly defined my {{{DATABASE}}} setting.

However, trying to syncdb throws
{{{
File "/home/artscoop/virtualenv/project/lib/python2.7/site-
packages/django/db/__init__.py", line 16, in <module>
router = ConnectionRouter(settings.DATABASE_ROUTERS)
File "/home/artscoop/virtualenv/project/lib/python2.7/site-
packages/django/db/utils.py", line 117, in __init__
raise ImproperlyConfigured('Error importing database router %s: "%s"'
% (klass_name, e))
django.core.exceptions.ImproperlyConfigured: Error importing database
router Router: "cannot import name connection"
}}}

No answer in any site could resolve this problem.
The dubious advice to add {{{from django.db import connections}}} leads to
another exception
{{{
File "/home/steve/virtualenv/project/lib/python2.7/site-
packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly
configured. "
ImproperlyConfigured: settings.DATABASES is improperly configured. Please
supply the ENGINE value. Check settings documentation for more details.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/20704>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 4, 2013, 5:31:22 PM7/4/13
to django-...@googlegroups.com
#20704: DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"
-------------------------------------+-------------------------------------

Reporter: artscoop | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: router | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by artscoop):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

I have found that moving the router to a file in the project root fixes
everything. But the problem with importing from another location is still
a bug.

--
Ticket URL: <https://code.djangoproject.com/ticket/20704#comment:1>

Django

unread,
Jul 4, 2013, 5:39:33 PM7/4/13
to django-...@googlegroups.com
#20704: DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"
-------------------------------------+-------------------------------------

Reporter: artscoop | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: router | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by aaugustin):

Looks like a circular import.

--
Ticket URL: <https://code.djangoproject.com/ticket/20704#comment:2>

Django

unread,
Jul 4, 2013, 5:47:36 PM7/4/13
to django-...@googlegroups.com
#20704: DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"
-------------------------------------+-------------------------------------

Reporter: artscoop | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: router | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by charettes):

@artscoop what are you importing in your routers module?

--
Ticket URL: <https://code.djangoproject.com/ticket/20704#comment:3>

Django

unread,
Jul 5, 2013, 2:47:01 AM7/5/13
to django-...@googlegroups.com
#20704: DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"
-------------------------------------+-------------------------------------

Reporter: artscoop | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: router | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by claudep):

This might have been fixed by commit [6a6bb168be90594a18]. To check this,
it would be nice if you could test with the current 1.6beta.

--
Ticket URL: <https://code.djangoproject.com/ticket/20704#comment:4>

Django

unread,
Jul 5, 2013, 8:12:48 AM7/5/13
to django-...@googlegroups.com
#20704: DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"
-------------------------------------+-------------------------------------

Reporter: artscoop | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: router | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by artscoop):

The routers.py module does not import anything.
Only has
{{{#!python
# coding: utf-8
from __future__ import absolute_import
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/20704#comment:5>

Django

unread,
Jul 5, 2013, 2:32:10 PM7/5/13
to django-...@googlegroups.com
#20704: DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"
-------------------------------------+-------------------------------------

Reporter: artscoop | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: router | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by artscoop):

The behaviour of routers seems correct in 1.6b1 though. (the only problem
there is with fixtures)

--
Ticket URL: <https://code.djangoproject.com/ticket/20704#comment:6>

Django

unread,
Jul 6, 2013, 3:43:47 AM7/6/13
to django-...@googlegroups.com
#20704: DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"
-------------------------------------+-------------------------------------
Reporter: artscoop | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: router | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

* status: new => closed
* resolution: => worksforme


Comment:

Thanks for testing on latest code, and I'm happy this is fixed there.

I think you should be able to temporarily workaround the problem by moving
your custom Router class in a different path. The current circular import
issue may be caused by any `__init__.py` along the path (i.e.
`engine/__init__.py`, `engine/db/__init__.py`, etc.).

I really doubt that the fix will be backported in 1.5, therefore closing
the ticket.

--
Ticket URL: <https://code.djangoproject.com/ticket/20704#comment:7>

Reply all
Reply to author
Forward
0 new messages