python 控制台错误输出未显示包含错误的行

mefy6pfw  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(195)

请我真的需要帮助,我不知道什么可能会发生在我的程序,initialy如果有一个错误,它只是简单地指出错误是在我的代码,例如。

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\python_files\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\python_files\venv\lib\site-packages\channels\management\commands\runserver.py", line 75, in inner_run
    self.check(display_num_errors=True)
  File "C:\python_files\venv\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = checks.run_checks(
  File "C:\python_files\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\python_files\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\python_files\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\python_files\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
    for pattern in self.url_patterns:
  File "C:\python_files\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\python_files\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\python_files\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\python_files\venv\lib\site-packages\django\urls\resolvers.py", line 582, in
 urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\python_files\hospital_project\urls.py", line 61, in <module>
    url('residence/', include('hospital_project.residence.urls', namespace="residence")),#dashboard for residence
  File "C:\python_files\venv\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\python_files\hospital_project\residence\urls.py", line 36, in <module>
    url(r'pharmacy1',  views.pharmacy1,  name="pharmacy"),
AttributeError: module 'hospital_project.residence.views' has no attribute 'pharmacy1'

从上面的错误中可以看出错误在文件C:\python_files\hospital_project\residence\urls.py的第36行
自从我使用python很长一段时间以来,这一直是正常的。但是突然我发现输出发生了变化。它没有给我文件中存在问题的特定行。而是将pythong文件中的整个代码打印到控制台上。请参见下面的示例。

--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 1081, in emit
    msg = self.format(record)
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 925, in format
    return fmt.format(record)
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 672, in format
    record.exc_text = self.formatException(record.exc_info)
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 622, in formatException
    traceback.print_exception(ei[0], ei[1], tb, None, sio)
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\traceback.py", line 103, in print_exception
    for line in TracebackException(
  File "C:\software_development\python\venv\lib\site-packages\trio\_core\_multierror.py", line 393, in traceback_exception_init
    traceback_exception_original_init(
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\traceback.py", line 523, in __init__
    self._load_lines()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\traceback.py", line 535, in _load_lines
    self.__context__._load_lines()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\traceback.py", line 535, in _load_lines
    self.__context__._load_lines()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\traceback.py", line 535, in _load_lines
    self.__context__._load_lines()
  [Previous line repeated 6 more times]
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\traceback.py", line 533, in _load_lines
    frame.line
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\traceback.py", line 288, in line
    self._line = linecache.getline(self.filename, self.lineno).strip()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\linecache.py", line 16, in getline
    lines = getlines(filename, module_globals)
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\linecache.py", line 47, in getlines
    return updatecache(filename, module_globals)
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\linecache.py", line 137, in updatecache
    lines = fp.readlines()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 6781: invalid start byte
Call stack:
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\threading.py", line 890, in _bootstrap
    self._bootstrap_inner()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\software_development\python\venv\lib\site-packages\django\utils\autoreload.py", line 53, i
n wrapper
    fn(*args, **kwargs)
  File "C:\software_development\python\venv\lib\site-packages\channels\management\commands\runserver.py", line 106, in inner_run
    self.server_cls(
  File "C:\software_development\python\venv\lib\site-packages\daphne\server.py", line 143, in run
    reactor.run(installSignalHandlers=self.signal_handlers)
  File "C:\software_development\python\venv\lib\site-packages\twisted\internet\asyncioreactor.py", li
ne 255, in run
    self._asyncioEventloop.run_forever()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 570, in run_forever
    self._run_once()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 1859, in _run_once
    handle._run()
  File "C:\Users\software_developer\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "C:\software_development\python\venv\lib\site-packages\twisted\internet\asyncioreactor.py", li
ne 271, in _onTimer
    self.runUntilCurrent()
  File "C:\software_development\python\venv\lib\site-packages\twisted\internet\base.py", line 991, in
 runUntilCurrent
    call.func(*call.args, **call.kw)
  File "C:\software_development\python\venv\lib\site-packages\daphne\server.py", line 290, in applica
tion_checker
    logger.error(
Message: 'Exception inside application: %s'
Arguments: (UnicodeDecodeError('utf-8', b'import re\r\nfrom django.db.models import Count, Avg\r\nfrom django.shortcuts import render, redirect\r\nfrom django.http 
import HttpResponse, HttpResponseRedirect, StreamingHttpResponse\r\n\r\nfrom django.urls import reverse...................the rest of the output are just my code
with \r \n \t included allover

请什么可能是怎么回事,我如何才能让程序产生错误。我真的很感激任何帮助,谢谢

luaexgnf

luaexgnf1#

日志的第一行告诉我们是logging python库中的emit方法有问题,所以问题是logging无法正确地将消息发送到您的终端,原因在下面:

File "C:\software_development\python\venv\lib\site-packages\daphne\server.py", line 290, in applica
tion_checker
    logger.error(
Message: 'Exception inside application: %s'
Arguments: (UnicodeDecodeError('utf-8', b'

因此,站点包daphne(可能是Django的依赖项)在记录消息时遇到了问题,因为它尝试记录的Arguments无法解码为UTF-8
从这个回溯中,如果没有实际的代码,很难说出确切的原因是什么,但是如果我是你,我会用调试器运行代码,一行一行地执行代码,然后检查问题在哪里。
您知道是哪个文件导致了问题,因为在您写入时,文件的全部内容都被记录下来了。
你应该小心你实际上记录的是什么。你记录了一些用户输入数据吗?一些你不能准确预测的DB内容吗?会有一些特殊的,非UTF-8字符吗?

相关问题