tags:

views:

256

answers:

2

The symptoms:

Could not import tinycomm.views. Error was: No module named models

The line of code:

 from tinycomm.models import *

Project structure:

/tinycomms
  /tinycomm
    models.py
    views.py
    etc.
  /other apps.

On Mac OS X in development I can fix this by putting tinycomms.tinycomm.models and then another similar error pops up elsewhere. I got development working by changing about 10 paths to models and forms in the app (many more remained just as tinycomm.module) by changing the path every time it fell over.

I uploaded to an Ubuntu production server and experienced the same problem, only the full name does not solve the problem.

This app has been working fine for months and the only major change that I can think of was to install Aptana Jaxer on Mac OS X, but as the problem also exists in production I am not sure that is the problem.

Other biggish change was to change the app folder name from 'web' to 'tinycomm' but with the full path fixes in development, it was working, so I am not sure that is the cause either.

Here is a list of the things I have tried:

  1. All the files it says it can't find are definitely where they should be
  2. init.py everywhere it should be
  3. paths to everywhere added /home/project_dir, /home/project_dir/tinycomms, /home/project_dir/tinycomms/tinycomm
  4. tried removing all these paths as well
  5. Tried taking out all the apps one by one - the modules that it complained about changed, but the basic No module named x remained the same.
  6. removed all the middleware etc.
  7. Upgraded to Django 1.2

Here is the full output:

Environment:

    Request Method: GET
    Request URL: http://127.0.0.1:8004/
    Django Version: 1.2 alpha 1
    Python Version: 2.5.2
    Installed Applications:
    ['django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.sites',
     'django.contrib.admin',
     'tinycomms_tagging',
     'tinycomm']
    Installed Middleware:
    ('django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django_authopenid.middleware.OpenIDMiddleware')


Traceback:
    File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
      90.                         request.path_info)
    File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/core/urlresolvers.py" in resolve
      222.                     sub_match = pattern.resolve(new_path)
    File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/core/urlresolvers.py" in resolve
      129.             return self.callback, args, kwargs
    File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/core/urlresolvers.py" in _get_callback
      138.             raise ViewDoesNotExist, "Could not import %s. Error was: %s" % (mod_name, str(e))

Exception Type: ViewDoesNotExist at /
Exception Value: Could not import tinycomm.views. Error was: No module named models

Any suggestions of what to try next gratefully received!

Contents of tinycomm/init.py:

from django.utils.translation import ugettext as _

from tinycomms_tagging.managers import ModelTaggedItemManager, TagDescriptor

VERSION = (0, 3, 'pre')

class AlreadyRegistered(Exception):
    """
    An attempt was made to register a model more than once.
    """
    pass

registry = []

def register(model, tag_descriptor_attr='tags',
             tagged_item_manager_attr='tagged'):
    """
    Sets the given model class up for working with tags.
    """
    if model in registry:
        raise AlreadyRegistered(
            _('The model %s has already been registered.') % model.__name__)
    registry.append(model)

    # Add tag descriptor
    setattr(model, tag_descriptor_attr, TagDescriptor())

    # Add custom manager
    ModelTaggedItemManager().contribute_to_class(model,
                                                 tagged_item_manager_attr)
+1  A: 

Inspect your code to see if somewhere you import starting from tinycomms.* (note the "s") instead of tinycomm.*. Sometimes when you mix relative imports with absolute imports you get strange errors like those.

prometheus
Didn't find any, but thanks for the tip.
+1  A: 

Make sure you're not importing tinycomm.views.* in tinycomm.models, circular dependencies like to throw these kinds of errors.

BTW, do you have something in your tinycomm/__init__.py?

EDIT:

The traceback suggests that it might come from your utls.py or one of it's include()s. You can try commenting out some of the url patterns and see what happens.

If that doesn't help, here's a simple way to hunt down circular imports (the non-obvious ones) and similar import problems that might be able to help you:

When you find which file is causing the import error, try removing (commenting out) all the import statements from that file: you should end up with a bunch of NameErrors. Then, start adding back the imports one by one, and when you get an ImportError instead of a NameError, just follow the import and do this with the file that is imported. Hope this helps.

Attila Oláh
Sadly no obvious circular dependancies but I'm going to go back through all the changes since the last version and see if there are any less obvious ones.Have edited the question to include what is in tinycomm.__init__.py, this has not been changed in a long time though.
Using Gitx reviewed all my changes and nothing that has added a circular reference. Put the old version back on production and development and they both work so definately some change that I made. Can't face any more frustration today - tomorrow I'll step through each of the many changes. Lesson learned - when developing create a git branch and save changes every night!
Ok, I've just updated the answer, maybe this time it will help you.
Attila Oláh
It's working and I'm not 100% clear why but I think it was overuse of 'tinycomm'. I followed your instructions until all the .py in the chain had only one import, except the last and it was still failing. Took out all the apps, middleware, still failing. Created a new app with a new name and started putting the items from tinycomm back in and it started working. Phew.
My conclusion is that I had an app called tinycomm, a .py file in another folder called tinycomm.py and a class within that callled TinyComm and even used a variable tinycomm in a subclass to refer to the parent. While this was ok most of the time, it broke the system in the end. Thank you so so much for putting me on the right track.