Django makemessages, TypeError: expected str, bytes or os.PathLike object, not list

Я пытаюсь создать многоязычный проект Django в pycharm. Но уже целый день пытаюсь понять, почему makemessages не генерирует .po файлы. Поэтому я обратился за помощью.

settings.py

from pathlib import Path
import os
from decouple import config
from unipath import Path
from django.utils.translation import gettext_lazy as _
import environ

env = environ.Env()
environ.Env.read_env()


BASE_DIR = Path(__file__).resolve().parent.parent
CORE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale/'),
)
LANGUAGE_CODE = 'en-us'

LANGUAGES = [
    ('en', _('English')),
    ('fr', _('French')),
]

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = [os.path.join(BASE_DIR, 'media')]

Когда я запускаю python manage.py makemessages -l fr, я получаю следующий ответ:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_manage.py", line 52, in <module>
    run_command()
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_manage.py", line 46, in run_command
    run_module(manage_file, None, '__main__', True)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 225, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/manage.py", line 22, in <module>
    main()
  File "/Users/famronnebratt/PycharmProjects/CompValid3/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/commands/makemessages.py", line 430, in handle
    potfiles = self.build_potfiles()
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/commands/makemessages.py", line 506, in build_potfiles
    file_list = self.find_files(".")
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/commands/makemessages.py", line 543, in find_files
    ignored_roots = [
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/commands/makemessages.py", line 544, in <listcomp>
    os.path.normpath(p)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/posixpath.py", line 336, in normpath
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not list

Process finished with exit code 1

The cause is not the LOCALE_PATHS. Django ignores the STATIC_ROOT [Django-doc] and MEDIA_ROOT [Django-doc]. Indeed:

def find_files(self, root):
    """
    Get all files in the given root. Also check that there is a matching
    locale dir for each file.
    """
    all_files = []
    ignored_roots = []
    if self.settings_available:
        ignored_roots = [
            os.path.normpath(p)
            for p in (settings.MEDIA_ROOT, settings.STATIC_ROOT)
            if p
        ]
    # …

По-видимому, это (непустые) списки в вашем settings.py.

Таким образом, вы должны переписать эти настройки так:

# settings.py

# …

STATIC_ROOT = [my_path1]
MEDIA_ROOT = [my_path2]

следует переписать так:

# settings.py

# …

STATIC_ROOT = my_path1
MEDIA_ROOT = my_path2

и то же самое с MEDIA_ROOT.

Благодаря комментариям willeM и Sunderam я нашел проблему.

Пакет django-import-export использует пакет xlwt. Для этого существует исправление. https://gist.github.com/federicobond/fd59c7b22b90795e26d2

Я обнаружил, что проще вручную заменить все

# -*- coding: windows-1252 -*-

с

# -*- coding: utf-8 -*-

в файлах .py в папке xlwt

Вернуться на верх