简体   繁体   中英

Django 2.1 and Python 3.7: KeyError: 'django' when running Apache2 mod_wsgi and other issues

I'm coming to you after so many hours trying to figure out why my django setup does not work anymore. I swear to whatever God you want I did not sleep last night because of it.

I wanted to switch from Python 3.5 to Python 3.7 because I started to use f-strings , and as this feature was available from Python 3.6, I was like why not using directly Python 3.7. Also, I wanted my 2 domains (mywebsite.fr and mywebsite.com) to point to the same django project. No issue with only mywebsite.fr until now.

I will try to explain as precisely as I can to you what I have done to obtain this hideous code below:

  • My install is on Ubuntu 16.04,
  • I did some mess with Python versions natively installed on my Ubuntu setup. I finally succeeded in installing properly (I think) Python 3.7,
  • I really struggled configuring/making/installing the appropriate mod_wsgi,
  • I use venv for my django setup (called m2gvenvprod below),
  • I am using mod_wgsi and its daemon mode associated,
  • With everything I read, I am sure my conf in apache2 for each of those website/domains are correct (in sites-available),
  • And here I am, with the apache2 error.log below, after trying any combination of everything I found on the internet regarding my issue. I am not able to understand that log. I do not understand why django is not a valid key. I know nothing for now... Truly desperate.

One thing maybe interesting I noticed: if I go to mywebsite.com: error 500. But if I go to mywebsite.com/admin, I am redirected to mywebsite.com/admin/login/?next=/admin/: so, there must be something still working, uh?

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 66, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
    module = import_module(entry[1])
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django_libs/templatetags/libs_tags.py", line 6, in <module>
    from django.template.base import TOKEN_BLOCK, TOKEN_VAR
ImportError: cannot import name 'TOKEN_BLOCK' from 'django.template.base' (/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/base.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/base.py", line 156, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/base.py", line 154, in _get_response
    response = response.render()
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/response.py", line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/response.py", line 63, in resolve_template
    return select_template(template, using=self.using)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/loader.py", line 38, in select_template
    engines = _engine_list(using)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/loader.py", line 66, in _engine_list
    return engines.all() if using is None else [engines[using]]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 81, in __getitem__
    engine = engine_cls(params)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 25, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
    for name in get_package_libraries(pkg):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 125, in get_package_libraries
    "trying to load '%s': %s" % (entry[1], e)
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'django_libs.templatetags.libs_tags': cannot import name 'TOKEN_BLOCK' from 'django.template.base' (/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/base.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 66, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
    module = import_module(entry[1])
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django_libs/templatetags/libs_tags.py", line 6, in <module>
    from django.template.base import TOKEN_BLOCK, TOKEN_VAR
ImportError: cannot import name 'TOKEN_BLOCK' from 'django.template.base' (/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/base.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/utils/deprecation.py", line 91, in __call__
    response = response or self.get_response(request)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 129, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
    template = loader.get_template(template_name)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/loader.py", line 12, in get_template
    engines = _engine_list(using)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/loader.py", line 66, in _engine_list
    return engines.all() if using is None else [engines[using]]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 81, in __getitem__
    engine = engine_cls(params)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 25, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
    for name in get_package_libraries(pkg):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 125, in get_package_libraries
    "trying to load '%s': %s" % (entry[1], e)
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'django_libs.templatetags.libs_tags': cannot import name 'TOKEN_BLOCK' from 'django.template.base' (/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/base.py)

Thank you in advance for the advice you could offer me!


EDIT 1

Now it is not working at all. I tried to make it work on Python 3.5 like before, but no success.

I also had trouble using apache with python 3.7.0.

Fortunately, django 2.1.4 worked correctly when combined with python 3.6.6.

Well, I would have never thought the official release of Django (2.1) would be the issue, but here we are! When creating my new virtual env under python 3.7 , I did not pay attention immediately to the new version of django. Even afterward, I supposed it was a stable release and that's it. So maybe something's wrong on my Ubuntu install (but really I can not see what), but downgrading from django 2.1 to django 2.0 just solved my issue! Happy even if I spent waaay too much time on that. Notice that it works just fine with Python 3.7 (just like my initial wish).

Thanks for reading!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM