Django makemessages报错TypeError:期望路径对象而非列表求助
Django makemessages 无法生成.po文件的解决方法
问题描述
在PyCharm中创建多语言Django项目时,执行python manage.py makemessages -l fr命令无法生成.po文件,报错如下:
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
项目的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')]
解决方法
错误根源在于MEDIA_ROOT的配置:你将它设置成了列表类型,但Django的makemessages命令在处理路径时,要求该参数为字符串类型。
修改settings.py中的MEDIA_ROOT配置,去掉方括号:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
修改完成后,重新执行命令:
python manage.py makemessages -l fr
此时应该能正常生成法语对应的.po文件。
内容的提问来源于stack exchange,提问作者Jonas
相关产品推荐
相关产品推荐

