python - 更新到 Django 2.2.4 和 python 3.7 (toml.py) 后无法运行 mange.py runserver

标签 python django django-2.2 python-3.7

当运行 python manage.py runserver --settings=project.settings_dev 我得到:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 95, in handle
    self.run(**options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 600, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 585, in start_django
    reloader.run(django_main_thread)
  File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 303, in run
    self.run_loop()
  File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 309, in run_loop
    next(ticker)
  File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 349, in tick
    for filepath, mtime in self.snapshot_files():
  File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 365, in snapshot_files
    for file in self.watched_files():
  File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 264, in watched_files
    yield from iter_all_python_module_files()
  File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 103, in iter_all_python_module_files
    return iter_modules_and_files(modules, frozenset(_error_files))
  File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 131, in iter_modules_and_files
    if spec.has_location:
AttributeError: 'str' object has no attribute 'has_location'

这是错误的函数: https://github.com/django/django/blob/master/django/utils/autoreload.py#L131

@functools.lru_cache(maxsize=1)
def iter_modules_and_files(modules, extra_files):
    """Iterate through all modules needed to be watched."""
    sys_file_paths = []
    for module in modules:
        # During debugging (with PyDev) the 'typing.io' and 'typing.re' objects
        # are added to sys.modules, however they are types not modules and so
        # cause issues here.
        if not isinstance(module, ModuleType):
            continue
        if module.__name__ == '__main__':
            # __main__ (usually manage.py) doesn't always have a __spec__ set.
            # Handle this by falling back to using __file__, resolved below.
            # See https://docs.python.org/reference/import.html#main-spec
            # __file__ may not exists, e.g. when running ipdb debugger.
            if hasattr(module, '__file__'):
                sys_file_paths.append(module.__file__)
            continue
        if getattr(module, '__spec__', None) is None:
            continue
        spec = module.__spec__
        # Modules could be loaded from places without a concrete location. If
        # this is the case, skip them.
        if type(spec) is str:
            import ipdb; ipdb.set_trace()
        if spec.has_location:
            origin = spec.loader.archive if isinstance(spec.loader, zipimporter) else spec.origin
            sys_file_paths.append(origin)

    results = set()
    for filename in itertools.chain(sys_file_paths, extra_files):
        if not filename:
            continue
        path = pathlib.Path(filename)
        try:
            if not path.exists():
                # The module could have been removed, don't fail loudly if this
                # is the case.
                continue
            results.add(path.resolve().absolute())
        except ValueError as e:
            # Network filesystems may return null bytes in file paths.
            logger.debug('"%s" raised when resolving path: "%s"' % (str(e), path))
    return frozenset(results)

django/utils/autoreload.py:131 中放置一个 ipdb 调试器之后,f if

type(spec) is str:
            import ipdb; ipdb.set_trace() 

并打印出 __module__.__name____module__.__file__ 我得到:

ipdb> module.__name__                                                                                                                                                                                                                                                           
'toml'
ipdb> module.__file__                                                                                                                                                                                                                                                           
'/usr/local/lib/python3.7/site-packages/toml.py'
ipdb>                                               

有谁知道是什么问题吗?

django/utils/autoreload.py:131 行替换为

if type(spec) is not str and spec.has_location:

开发服务器正在以某种方式运行...

最佳答案

pip install toml==0.10.0

将 toml 更新到 PyPi 的最新版本有效 https://pypi.org/project/toml/

关于python - 更新到 Django 2.2.4 和 python 3.7 (toml.py) 后无法运行 mange.py runserver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57630716/

相关文章:

python - Django SELECT 语句,排序依据

python - Django:Selenium-浏览器上的陈旧元素引用

python - 使用循环在 Python 中反转字符串?

python - PyQt4 在退出时崩溃

Django 密码重置。不发送邮件

django-2.2 - 如何在Django中的特定时间后自动删除数据库中的记录

python - django 唯一对象(不是唯一字段)?

python - 如何解决类型错误: create_superuser() got an unexpected keyword argument 'paid' in django

python - 脚本中的 virtualenv 目录

Python:将二维列表 append 到另一个二维列表