2
votes

My application was working fine when I wanted to see whether I could organize my project in a better way. I read through this tutorial on structuring a django project.

Before my project structure was as follows:

  • camucamu
    • books
      • admin.py
      • models.py
      • views.py
      • __init__.py
    • static
    • templates
    • urls.py
    • views.py
    • settings.py
    • wsgi.py
    • __init__.py

What I wanted to do was move the books app into an apps folder. Thus I did that and changed the project structure to the following:

  • camucamu
    • apps
      • books
        • admin.py
        • models.py
        • views.py
        • __init__.py
    • static
    • templates
    • urls.py
    • views.py
    • settings.py
    • wsgi.py
    • __init__.py

I then changed the imports in views.py and admin.py from books.models to apps.books.models. I also changed INSTALLED_APPS in settings.py from books to apps.books.

When I then tried to run syncdb, I get the following error:

raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
django.core.exceptions.ImproperlyConfigured: ImportError apps.books: No module named apps.books

What am I messing up here so it can't find my app anymore?

2
did you change the path in manage.py?Ahsan
@Ahsan Why would I need to do that? The manage.py file just references the settings file which is still in the same placeBrian Fabian Crain

2 Answers

4
votes

Your apps folder does not have an __init__.py file so it cannot be recognized as a python module

4
votes

I got the same error, following the same guide, as the last point of the following list was not cited. Make sure you performed the following changes:

  • Create a blank __init__.py file inside the apps folder (needed for python to recognize it as a package)
  • Update the import statements wherever you refer to an external app:

    from projectname.apps.appname.models import YourModel, YourOtherModel
    
  • Inside settings.py edit INSTALLED_APPS such that it looks like this:

    INSTALLED_APPS = (
    
        ...
    
        # apps
        'projectname.apps.appname1',
        'projectname.apps.appname2',
    )
    
  • This one is not specified in the guide: In all your urls.py files, update the urlpatterns!

    BEFORE:

    # client views
    urlpatterns += patterns('appname',
        ...
    )
    

    AFTER:

    # client views
    urlpatterns += patterns('projectname.apps.appname',
        ...
    )
    
  • Finally remember to update your changes by calling python manage.py syncdb

Hope that helped.