fgallina / python-django.el

An Emacs package for managing Django projects.
GNU General Public License v3.0
67 stars 24 forks source link

Error importing packages with (weird?) AppConfig names/paths #22

Closed fritz-k closed 9 years ago

fritz-k commented 9 years ago

django-rules (https://github.com/dfunckt/django-rules/) recommends loading the packages with

INSTALLED_APPS = (
    'rules.apps.AutodiscoverRulesConfig',
)

which leads to an ImportError on running python-django-open-project

Details: 

Error executing: /home/fritz/.pyvenvs/intranet/bin/python -c "
from __future__ import print_function
import os
import sys
from os.path import dirname, abspath
stdout = sys.stdout; stderr = sys.stderr
sys.stdout = sys.stderr = open(os.devnull, 'w')
from django.conf import settings
# Try to import json really hard
try:
    import json
except ImportError:
    from django.utils import simplejson as json
# Force settings loading so all output is sent to devnull.
settings.DEBUG
sys.stdout = stdout; sys.stderr = stderr

app_paths = {}
for app in settings.INSTALLED_APPS:
    mod = __import__(app)
    if '.' in app:
        for sub in app.split('.')[1:]:
            mod = getattr(mod, sub)
    app_paths[app] = dirname(abspath(mod.__file__))
print(json.dumps(app_paths), end='')"

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 20, in <module>
ImportError: No module named 'rules.apps.AutodiscoverRulesConfig'; 'rules.apps' is not a package

I'm not quite sure if this is good practice on django-rules' part or not, but would appreciate your help if there's an easy solution. For the time being, I use a dirty hack

"app_paths = {}\n"
"for app in settings.INSTALLED_APPS:\n"
"    if 'rules' in app.split('.'):\n"
"        app = 'rules'\n"
"    ..."

Regards

fgallina commented 9 years ago

Pushed a fix for this in 433b132

Thanks for the report, Fabián.

fritz-k commented 9 years ago

Thanks for the quick fix!

This messes up the displayed order of apps though. I wrote a fix in #23.