开发者

Broke my Django app - a path problem?

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 AlreadyReg开发者_开发百科istered(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)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜