Search code examples
pythonpycharmpytesttqdm

tqdm and pytest INTERNALERROR> UnicodeEncodeError: 'charmap' codec can't encode characters in position 245-254: character maps to <undefined>


MRE:

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

Running the test through PyCharm raises the following error:

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

Running pytest with the terminal works though.

The problem does seem to come from tqdm.

I'm using Python 3.11.1 and:

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)

As @aaron pointed out, there might be a huge error on the stack trace from PyCharm because when I click on About it does show 2022.3.1 and when I run the code the stack trace says 2022.1.3.


Solution

  • It is using the cp1252.py-encoding which works until it finds a sequence it can't make sense of with this encoding (as you pointed out probably related to tqdm). The encoding is related to Windows, so it makes sense that it couldn't be reproduced by aaron (macOS) and me (Linux).

    If you can give it parameters to the execution of python, then this could be helpful. The idea is to force it to use utf-8 encodings with the argument -X utf8. You can add that argument under Interpreter Options in Run -> Edit Configurations... for your py-test.

    Debug Configurations


    It seems that it just takes the default encoding in windows, you can check if this is indeed the case on your system in the PowerShell:

    [System.Text.Encoding]::Default
    

    There are multiple issues on the github-page of this plugin, this workaround looks worth a try to me:

    "Just add the following line into the beginning of all files with tests:"

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

    Maybe you can try this too, but I think it's only for the console output and does not overwrite the default for the plugins too.

    1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Editor | General | Console.
    2. Select the default encoding from the Default Encoding list. (change it to utf-8)
    3. Click OK to apply the changes.