ImportError: cannot import name 'text_encoding' from 'io' (unknown location)

Hello Python community,

Recently I encountered this python error when try to run some tests on Ubuntu and Windows Python 3.10.4 using pytest module:

Traceback (most recent call last):
E       File "/usr/lib/python3.10/io.py", line 54, in <module>
E     ImportError: cannot import name 'text_encoding' from 'io' (unknown location)

Tried to search on internet but the only related “solution” is to reinstall python, I tried but did not work. Hope someone can help, thank you!
The Windows full error is here.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

result = <ExecutionResult object at 0x000001EAAAC>
check_error_string = True

    @staticmethod
    def RaiseIfError(result, check_error_string=True):
      """Raise an Execution error if the result of the process indicates failure.
    
      Args:
        result: ExecutionResult, The result of the process.
        check_error_string: bool, True to raise if 'ERROR' is found in stdout or
          stderr.  False to raise on non-zero exit code only.
    
      Raises:
        ExecutionError: If failure was detected for the process.
      """
      fail = result.return_code != 0
      if check_error_string:
        fail |= result.stdout is not None and 'ERROR' in result.stdout
        fail |= result.stderr is not None and 'ERROR' in result.stderr
      if fail:
>       raise ExecutionError(result)
E       ExecutionError: Command failed:
E       Command:
E       	cmd /c some command
E       Exit Code:
E       	1

E       
E       Stderr:
E       Fatal Python error: init_sys_streams: can't initialize sys standard streams
E       Python runtime state: core initialized
E       Traceback (most recent call last):
E         File "C:\Program Files\Python310\lib\io.py", line 54, in <module>
E       ImportError: cannot import name 'text_encoding' from 'io' (unknown location)

lib\tests\lib\exec_utils.py:154: ExecutionError

Why is there a unix error message if you running on microsoft windows?
Are you using WSL?

My bad, this is not only an error on Windows but also on linux.

The error messages refer to this file in the standard library. As you can see, the code says that it is importing from _io (meaning, a C implementation of the actual IO logic) rather than io (the name in the error message). Does it look that way in your local versions of those files (please do not try to edit them)?

This is very strange. My first thought was that, although it is a strange name to use, a file in the project’s code called _io.py could cause a problem like that because of:

However, I don’t think it can cause this problem, because the _io module is supposed to be built-in (directly in the Python executable, not a .pyd file). So Python should be able to import that (and use the proper module) directly, without trying to look in sys.path to find it.

It would make sense that io.py could not find those names by trying to import from itself - because it doesn’t define them locally (that’s why it’s importing the builtin - it wraps the parts that need C to implement, to give a nice Python interface). But the source code shouldn’t be saying to do that, certainly not after reinstalling.