Import problem: app name cannot be the same as project name?

64 views
Skip to first unread message

Edwin

unread,
Jan 21, 2011, 3:36:29 PM1/21/11
to Django users
I have an app that's called the same way as my project name (let's
name it 'blog'). My directory structure is:

blog
apps
blog
models.py
books
management
models.py


Everything works fine except that when i created a custom command
inside another app, call it 'books', Django can't find it.. as it
turns out, somehow django gets confused and the project root path for
searching apps in the management tool
(django.core.management.__init__.py) becomes blog.apps.blog instead of
just blog as the project name. This results in django can't find of my
custom commands inside books app because it's searching on a wrong
path!

One thing to mention is that in my INCLUDED_APPS, i define my apps
using its project name as well:
INCLUDED_APPS = (
'blog.apps.blog',
'blog.apps.books',
)

This is also one reason why Django can't find my custom command i
guess... but i need to define full path (including project name) here
otherwise I get errors when my AlreadyRegistered from admin because
admin.py is imported twice with different method (when I don't specify
project name in settings.py, django tries to import admin.py using
'apps.blog.admin' and I'm importing it using
'blog.apps.blog.admin')...

My use of import is probably the root cause of everything.. I've heard
various people say that do relative import within apps so that they're
reusable but many Python devs say that always use absolute import. So
tried using absolute import all the time...

I know my question is a bit all over the place here... but can anyone
suggest what to do?

Thanks!

bruno desthuilliers

unread,
Jan 22, 2011, 5:46:41 AM1/22/11
to Django users


On 21 jan, 21:36, Edwin <edwinja...@gmail.com> wrote:
> I have an app that's called the same way as my project name

Won't work.


(let's
> name it 'blog'). My directory structure is:
>
> blog
>    apps
>       blog
>           models.py
>       books
>           management
>           models.py
>
> Everything works fine except that when i created a custom command
> inside another app, call it 'books', Django can't find it.. as it
> turns out, somehow django gets confused

A Django project is a Python package, a Django app is Python package,
so if they are named the same one is always going to shadow the other.
Read about the Python's module search path and you'll understand why.
So it's mostly a Python problem, not a Django one, even if the way
Django imports the app's models at startup doesn't help.

> I know my question is a bit all over the place here... but can anyone
> suggest what to do?

Obviously, rename your project ;)

> when I don't specify project name in settings.py, django tries to import admin.py using 'apps.blog.admin' and I'm importing it using
'blog.apps.blog.admin'

You should only refer to the app's name when doing imports, ie
"blog.admin', 'books.models' etc.
Reply all
Reply to author
Forward
0 new messages