Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30364 closed Bug (invalid)

Django 2.2 check fails under sourceless project

Reported by: Sascha P. Owned by: nobody
Component: Uncategorized Version: 2.2
Severity: Normal Keywords: compileall, Django 2.2, encoding
Cc: Florian Apolloner Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


We are shipping python in docker containers in bytecode for legal purposes and obfuscation. With the new Django 2.2 this stopped working, due to an encoding issue. To reproduce this error we receive, we created a minimal dockerfile:

FROM python:alpine3.6
RUN apk upgrade --no-cache && \
    pip install Django==2.2.0 && \
    django-admin startproject mysite && \
    python3 mysite/ makemigrations && \
    python3 -m compileall -b -f mysite && \
    find mysite -type f -name '*.py' | xargs rm && \
    mv mysite/manage.pyc mysite/ && \
    python3 mysite/ makemigrations
CMD /bin/sh

You will get this error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/template/", 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 "mysite/", line 21, in <module>
  File "mysite/", line 17, in main
  File "/usr/local/lib/python3.6/site-packages/django/core/management/", line 381, in execute_from_command_line
  File "/usr/local/lib/python3.6/site-packages/django/core/management/", line 375, in execute
  File "/usr/local/lib/python3.6/site-packages/django/core/management/", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/", line 361, in execute
  File "/usr/local/lib/python3.6/site-packages/django/core/management/", line 390, in check
  File "/usr/local/lib/python3.6/site-packages/django/core/management/", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/core/checks/", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/", line 80, in check_dependencies
    for engine in engines.all():
  File "/usr/local/lib/python3.6/site-packages/django/template/", line 90, in all
    return [self[alias] for alias in self]
  File "/usr/local/lib/python3.6/site-packages/django/template/", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/usr/local/lib/python3.6/site-packages/django/template/", line 81, in __getitem__
    engine = engine_cls(params)
  File "/usr/local/lib/python3.6/site-packages/django/template/backends/", line 23, in __init__
    options.setdefault('file_charset', settings.FILE_CHARSET)
  File "/usr/local/lib/python3.6/site-packages/django/conf/", line 134, in FILE_CHARSET
    stack = traceback.extract_stack()
  File "/usr/local/lib/python3.6/", line 207, in extract_stack
    stack = StackSummary.extract(walk_stack(f), limit=limit)
  File "/usr/local/lib/python3.6/", line 358, in extract
  File "/usr/local/lib/python3.6/", line 282, in line
    self._line = linecache.getline(self.filename, self.lineno).strip()
  File "/usr/local/lib/python3.6/", line 16, in getline
    lines = getlines(filename, module_globals)
  File "/usr/local/lib/python3.6/", line 47, in getlines
    return updatecache(filename, module_globals)
  File "/usr/local/lib/python3.6/", line 137, in updatecache
    lines = fp.readlines()
  File "/usr/local/lib/python3.6/", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8d in position 4: invalid start byte

If you use Django==2.1.8 instead you will get the expected "No changes detected" and everything works fine. There has been some work around "FILE_CHARSET" with Django 2.2.0, so maybe thats where this comes from.

We normally always use the newest Django version, yet with this one we had to pin the version to 2.1.8.

If this is not a bug caused by Django, then Im sorry for reporting this, yet I hope that someone could point me to the right direction.

Best regards

Change History (5)

comment:1 Changed 5 years ago by Florian Apolloner

Resolution: invalid
Status: newclosed

Renaming a .pyc file to .py is not anything that is supposed to work.

comment:2 Changed 5 years ago by Tim Bell

Summary: python3 compileall not working anymore with Django 2.2Django 2.2 check fails under sourceless project

The deprecation check for settings.FILE_CHARSET added in #29817 uses traceback.extract_stack() to find out where the setting is being accessed from. When a project's .py files have been deleted, leaving only the .pyc files, extract_stack() throws an exception as it tries to read the .pyc file.

Running check will tickle this "bug" when it checks the template engines.

comment:3 Changed 5 years ago by Florian Apolloner

That is not exactly true, at least not in this case. In this case the issue is caused by the fact that they rename the pyc file to py which extract_stack then fails on because a py file is supposed to be readable…

comment:4 Changed 5 years ago by Florian Apolloner

Cc: Florian Apolloner added

comment:5 Changed 5 years ago by Sascha P.

Thanks at everyone. At least I know now into which direction to investigate.

Note: See TracTickets for help on using tickets.
Back to Top