respondcreate / django-versatileimagefield

A drop-in replacement for django's ImageField that provides a flexible, intuitive and easily-extensible interface for quickly creating new images from the one assigned to the field.
http://django-versatileimagefield.readthedocs.io/
MIT License
531 stars 88 forks source link

import_module blows up when it can't import something in INSTALLED_APPS #32

Closed decentral1se closed 8 years ago

decentral1se commented 8 years ago

Hi there, not sure if this is a problem in django-versatileimagefield or django-rules, but I don't really understand what is happening, so I will start here :+1:

I have a 'rules.apps.AutodiscoverRulesConfig' (docs) line in my INSTALLED_APPS which is some django-rules specific configuration for discovering my rules. However, your implementation of autodiscover() will call import_module and blow up when it can't import the AutodiscoverRulesConfig.

Any ideas what the fix should be?

The error I am seeing is below.

Traceback (most recent call last):
  File "./manage.py", line 8, in <module>
    execute_from_command_line(sys.argv)
  File "/home/lwm/velodrome/.venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/home/lwm/velodrome/.venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 324, in execute
    django.setup()
  File "/home/lwm/velodrome/.venv/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/lwm/velodrome/.venv/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/lwm/velodrome/.venv/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/lwm/velodrome/velodrome/lock8/models.py", line 80, in <module>
    from versatileimagefield.fields import VersatileImageField
  File "/home/lwm/velodrome/.venv/src/django-versatileimagefield/versatileimagefield/fields.py", line 13, in <module>
    from .files import VersatileImageFieldFile, VersatileImageFileDescriptor
  File "/home/lwm/velodrome/.venv/src/django-versatileimagefield/versatileimagefield/files.py", line 11, in <module>
    from .mixins import VersatileImageMixIn
  File "/home/lwm/velodrome/.venv/src/django-versatileimagefield/versatileimagefield/mixins.py", line 12, in <module>
    autodiscover()
  File "/home/lwm/velodrome/.venv/src/django-versatileimagefield/versatileimagefield/registry.py", line 196, in autodiscover
    mod = import_module(app)
  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named 'rules.apps.AutodiscoverRulesConfig'; 'rules.apps' is not a package
respondcreate commented 8 years ago

Hey @lwm!

Did you install django-rules or rules?

If you ran $ pip install django-rules, the 0.2 of django-rules will install into your python path. The 0.2 release doesn't have a 'apps.py' module (relevant code link here). Post the 0.2 release, the from-pip installation command changed to: $ pip install rules (note the lack of django- prior to rules). If you run that it'll install version 1.1.1 (the latest release) and will work as expected.

Here's the TL;DR fix:

$ pip uninstall django-rules
$ pip install rules
decentral1se commented 8 years ago

Hey @respondcreate, thanks for quick response.

Unfortunately, this isn't working, as I am running on rules=1.1.1 version and this still blows up.

lwm$ pip uninstall django-rules
Cannot uninstall requirement django-rules, not installed
Storing debug log for failure in /home/lwm/.pip/pip.log
(.venv) ruby-2.2.1 ~/velodrome (versatile-image) [05:10:26]
lwm$ pip install rules
Requirement already satisfied (use --upgrade to upgrade): rules in ./.venv/lib/python3.4/site-packages
Cleaning up...
(.venv) ruby-2.2.1 ~/velodrome (versatile-image) [05:10:29]
lwm$ pip install rules --upgrade
Requirement already up-to-date: rules in ./.venv/lib/python3.4/site-packages
Cleaning up...

Any other ideas?

respondcreate commented 8 years ago

Hey @lwm –

I think I figured out the source of the issue. I was able to reproduce & fix it locally, would you mind installing the code I wrote on this bug fix branch and then test it on your end as well? Here's how to get it:

$ pip uninstall django-versatileimagefield
$ pip install https://github.com/WGBH/django-versatileimagefield/archive/autodiscover-bug-34.zip

If it fixes things on your end as well then I'll merge it into master and do a official release to PyPI.

Thanks for your patience!

decentral1se commented 8 years ago

Awesome, that worked for me, thanks :+1: :rocket:

respondcreate commented 8 years ago

Hey @lwm–

The fix for this is available in the 1.2.2 release which I just uploaded to PyPI. Get it via pip:

$ pip install django-versatileimagefield==1.2.2

Thanks so much for your contribution to this project! :)