Opened 11 years ago

Closed 11 years ago

#21176 closed Uncategorized (invalid)

Import problem when executing collectstatic command

Reported by: anonymous Owned by: nobody
Component: Uncategorized Version: 1.5
Severity: Normal Keywords: circular, reference, import, debug, false, true
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Is it possible to make the import mechanism the same for debug=True and debug=False?

The problem is everything runs fine on development but when you try to deploy to stable, import problems suddenly arise. While I agree it's caused by some circular references but it's hard to debug at the later stage in development and they are hard to spot, not to mention using external applications.

Change History (13)

comment:1 by james@…, 11 years ago

comment:2 by Tim Graham, 11 years ago

Can you provide the actual error and details to reproduce?

comment:3 by Marc Tamlyn, 11 years ago

Resolution: duplicate
Status: newclosed

It will be a circular import between two modules. Sometimes they import in one order, sometimes in the other. This is partly a function of the quirks of the python import system, partly the lack of a unified entry point in Django.

Yes, it is possible to fix, but it isn't that simple. This is generally a nice side effect of most proposed implementations of the app refactor #3591. I'm not sure if it can be fixed without that. It is a change is how the app cache is loaded and all the various modules are imported.

This change would fix it, but more importantly don't do circular imports!

in reply to:  2 comment:4 by anonymous, 11 years ago

Replying to timo:

Can you provide the actual error and details to reproduce?

The problem on my setup is kind of specific and complex and I am unable to open up code for public to view, so I am afraid is hard to reproduce the exact same issue, and I am not actually sure which line of the code is causing the circular reference problem, so just adds more difficulties to show you.

However, I am not the only one who is suffering this problem. http://stackoverflow.com/questions/4970489/what-could-cause-a-django-error-when-debug-false-that-isnt-there-when-debug-tru
(The allowed_host answer is irrelevant here)

in reply to:  3 comment:5 by anonymous, 11 years ago

Replying to mjtamlyn:

It will be a circular import between two modules. Sometimes they import in one order, sometimes in the other. This is partly a function of the quirks of the python import system, partly the lack of a unified entry point in Django.

Yes, it is possible to fix, but it isn't that simple. This is generally a nice side effect of most proposed implementations of the app refactor #3591. I'm not sure if it can be fixed without that. It is a change is how the app cache is loaded and all the various modules are imported.

This change would fix it, but more importantly don't do circular imports!

Believe me, making circular import is the last thing I would want to do, all I wanted, is to be notified in development stage.

comment:6 by anonymous, 11 years ago

Resolution: duplicate
Status: closednew

comment:7 by anonymous, 11 years ago

Can this not be closed, but instead, set as related to whatever tickets that have the same problem?

comment:8 by Aymeric Augustin, 11 years ago

We prefer keeping only one ticket per problem, to avoid spreading the discussion over multiple tickets.

Is there a particular reason to keep this one open?

comment:9 by anonymous, 11 years ago

I will try to explain what I think it might cause the problem:

This app https://bitbucket.org/jameslin/zencoder-public which being used by 2 other apps. Typical references look like this:

from zencode.signals import zencode
post_save.connect(zencode, Video, dispatch_uid="videos.models.video")

Class Video(models.Model):

videos = generic.GenericRelation('zencode.ZencodedVideo', related_name="zen_videos")

Everything works fine on developmetn but when I deploy to stable, I get problems like cannot import zencode.

Normally it would still work on stable if I only have 1 app that uses the zencode app, but since there are 2 apps in this case, it's causing some circular import

comment:10 by anonymous, 11 years ago

Also, I think the Site.objects.get_current() plays an important role here to trigger the problem

Traceback (most recent call last):
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/management/base.py", line 222, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute
    output = self.handle(*args, **options)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/management/base.py", line 385, in handle
    return self.handle_noargs(**options)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 164, in handle_noargs
    collected = self.collect()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
    handler(path, prefixed_path, storage)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 285, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 224, in delete_file
    source_last_modified = source_storage.modified_time(path)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/files/storage.py", line 277, in modified_time
    return datetime.fromtimestamp(os.path.getmtime(self.path(name)))
  File "/srv/www/weddings/local/lib/python2.7/site-packages/dajaxice/finders.py", line 55, in path
    path = self.get_or_create_file(name)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/dajaxice/finders.py", line 21, in get_or_create_file
    data = getattr(self, self.files[path])()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/dajaxice/finders.py", line 68, in dajaxice_core_js
    dajaxice_autodiscover()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/dajaxice/core/Dajaxice.py", line 135, in dajaxice_autodiscover
    import_module("%s.ajax" % app)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/srv/www/weddings/weddings/directory/ajax.py", line 3, in <module>
    from views.index import filter_content
  File "/srv/www/weddings/weddings/directory/views/index.py", line 4, in <module>
    from directory.models import *
  File "/srv/www/weddings/weddings/directory/models.py", line 26, in <module>
    from zencode.signals import zencode
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django_mash_zencode-0.1-py2.7.egg/zencode/signals.py", line 6, in <module>
    site = Site.objects.get_current()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/sites/models.py", line 26, in get_current
    current_site = self.get(pk=sid)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/query.py", line 391, in get
    clone = self.filter(*args, **kwargs)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/query.py", line 667, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/query.py", line 685, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1259, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1127, in add_filter
    process_extras=process_extras)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1313, in setup_joins
    field, model, direct, m2m = opts.get_field_by_name(name)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/options.py", line 351, in get_field_by_name
    cache = self.init_name_map()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/options.py", line 380, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/options.py", line 469, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/options.py", line 483, in _fill_related_many_to_many_cache
    for klass in get_models(only_installed=False):
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/loading.py", line 197, in get_models
    self._populate()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/loading.py", line 75, in _populate
    self.load_app(app_name)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/loading.py", line 96, in load_app
    models = import_module('.models', app_name)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/srv/www/weddings/weddings/videos/models.py", line 8, in <module>
    from zencode.signals import zencode
ImportError: cannot import name zencode

comment:11 by anonymous, 11 years ago

in this case, the 2 apps I mentioned above are "directory" and "video"

comment:12 by anonymous, 11 years ago

Summary: Import problem only occurs when debug=FalseImport problem when executing collectstatic command

comment:13 by anonymous, 11 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top