Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Import problem: app name cannot be the same as project name? [RSS Feed]

#1 Jan. 22, 2011 02:23:01

E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


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!

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 22, 2011 10:46:49

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


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.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Import problem: app name cannot be the same as project name? [RSS Feed]

Board footer

Moderator control

Enjoy the 13th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support