python tqdm和pytest内部错误>Unicode编码错误:“charmap”编解码器无法编码位置245-254中的字符:字符Map到< undefined>

bnlyeluc  于 2023-01-12  发布在  Python
关注(0)|答案(1)|浏览(169)

先生:
test_test.py

import main

def test_file() -> None:
    main.main("qfqsdfsqdds")
    assert True

main.py

from tqdm import tqdm

def main(username: str):
    for i in tqdm(range(10)):
        a = i + 1

通过PyCharm运行测试会引发以下错误:

test_test.py::test_file PASSED                                           [100%]
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 270, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>                          ^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 324, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>           ^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 349, in pytest_runtestloop
INTERNALERROR>     item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>           ^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 112, in pytest_runtest_protocol
INTERNALERROR>     runtestprotocol(item, nextitem=nextitem)
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 131, in runtestprotocol
INTERNALERROR>     reports.append(call_and_report(item, "call", log))
INTERNALERROR>                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 224, in call_and_report
INTERNALERROR>     hook.pytest_runtest_logreport(report=report)
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>           ^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\teamcity\pytest_plugin.py", line 300, in pytest_runtest_logreport
INTERNALERROR>     self.report_test_output(report, test_id)
INTERNALERROR>   File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\teamcity\pytest_plugin.py", line 208, in report_test_output
INTERNALERROR>     dump_test_stderr(self.teamcity, test_id, test_id, data)
INTERNALERROR>   File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\teamcity\common.py", line 78, in dump_test_stderr
INTERNALERROR>     messages.testStdErr(test_id, chunk, flowId=flow_id)
INTERNALERROR>   File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\teamcity\messages.py", line 190, in testStdErr
INTERNALERROR>     self.message('testStdErr', name=testName, out=out, flowId=flowId)
INTERNALERROR>   File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_runner_tools.py", line 117, in message
INTERNALERROR>     _old_service_messages.message(self, messageName, **properties)
INTERNALERROR>   File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\teamcity\messages.py", line 101, in message
INTERNALERROR>     retry_on_EAGAIN(self.output.write)(self.encode(message))
INTERNALERROR>                                        ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\teamcity\messages.py", line 68, in encode
INTERNALERROR>     value = value.encode(self.encoding)
INTERNALERROR>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\lhott\AppData\Local\Programs\Python\Python311\Lib\encodings\cp1252.py", line 12, in encode
INTERNALERROR>     return codecs.charmap_encode(input,errors,encoding_table)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> UnicodeEncodeError: 'charmap' codec can't encode characters in position 245-254: character maps to <undefined>

============================== 1 passed in 0.02s ==============================

Process finished with exit code 3

不过,在终端上运行pytest是可行的。
问题似乎确实来自tqdm
我使用Python 3.11.1和:

PyCharm 2022.3.1 (Community Edition)
Build #PC-223.8214.51, built on December 20, 2022
Runtime version: 17.0.5+1-b653.23 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2030M
Cores: 16
Non-Bundled Plugins:
    me.lensvol.blackconnect (0.5.0)
    com.chesterccw.excelreader (2022.12.1-203.223)
    com.github.copilot (1.1.38.2229)

正如@aaron所指出的,PyCharm的堆栈跟踪可能有一个巨大的错误,因为当我单击"关于"时,它确实显示2022.3.1,而当我运行代码时,堆栈跟踪显示2022.1.3

zphenhs4

zphenhs41#

它使用cp1252.py-编码,直到它找到一个序列,它不能用这个编码(正如你指出的可能与tqdm有关)。编码与Windows有关,所以它不能被aaron(macOS)和我(Linux)复制是有道理的。
如果你能给它python执行的参数,那么this可能会有帮助,这个想法是强制它使用utf-8编码,参数为-X utf8,你可以在Run -> Edit Configurations...中的Interpreter Options下添加这个参数,用于py-test。

看起来它只需要default encoding in windows,您可以在PowerShell中检查您的系统上是否确实存在这种情况:

[System.Text.Encoding]::Default

这个插件的github页面上有很多问题,这个变通方案看起来值得一试:
"只需将以下行添加到所有包含测试的文件的开头:"

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

也许你也可以试试这个,但是我认为它只适用于控制台输出,并且不会覆盖插件的默认值。
1.在"设置/首选项"对话框(Ctrl + Alt + S)中,选择"编辑器|一般情况|控制台。
1.从默认编码列表中选择默认编码。(将其更改为utf-8)
1.单击"确定"应用更改。

相关问题