首页 > 解决方案 > Django“ValueError:嵌入的空字节”

问题描述

我刚刚安装了 Django 并在 settings.py 中将 DATABASES 从 sqlite3 更改为 mysql,但是当我运行服务器时,偶尔会出现“ValueError:embedded null byte”。我应该怎么办?

(myproject) bash-3.2$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 95, in handle
    self.run(**options)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 577, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 562, in start_django
    reloader.run(django_main_thread)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 280, in run
    self.run_loop()
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 286, in run_loop
    next(ticker)
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 326, in tick
    for filepath, mtime in self.snapshot_files():
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 342, in snapshot_files
    for file in self.watched_files():
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 241, in watched_files
    yield from iter_all_python_module_files()
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 103, in iter_all_python_module_files
    return iter_modules_and_files(modules, frozenset(_error_files))
  File "/Users/mydir/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 132, in iter_modules_and_files
    results.add(path.resolve().absolute())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py", line 1122, in resolve
    s = self._flavour.resolve(self, strict=strict)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py", line 349, in resolve
    return _resolve(base, str(path)) or sep
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py", line 331, in _resolve
    target = accessor.readlink(newpath)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py", line 443, in readlink
    return os.readlink(path)
ValueError: embedded null byte

标签: pythondjangopython-3.x

解决方案


我有同样的问题,调试它看起来是由 v2.2.0 中引入 watchman 引起的,因为降级到 v1.9.0 会删除它。

奇怪的是,有时服务器会启动,但似乎并非每次都发生错误,但我相当确定这是由于虚拟环境造成的。

修复:

禁用重新加载(不理想)

python manage.py runserver --noreload

创建一个守望者配置并忽略 venv

https://docs.djangoproject.com/en/2.2/ref/django-admin/#runserver

https://facebook.github.io/watchman/docs/config.html#ignore_dirs

但是我实际上无法让 ignore_dirs 工作......

姜戈票

https://code.djangoproject.com/ticket/30506


推荐阅读