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.

1 Like

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.

So… What is the solution?
I‘m using Ubuntu 22.04.3 with Python 3.10.12.

➜  ~ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy
➜  ~ python --version
Python 3.10.12
➜  ~ which python
/usr/bin/python

And my ERROR message:

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

Thank you …

You did not share you code so I’m not sure why you see the error.

For me this works:

% python3.10
Python 3.10.11 (v3.10.11:7d4cc5aa85, Apr  4 2023, 19:05:19) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from io import text_encoding
>>>

This works for me as well…

➜  ~ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from io import text_encoding
>>> 

Please take a look a this https://github.com/FPGAwars/apio/issues/334

Thank you …

That apio issue report doesn’t really give more info and seems unrelated to apio (though I don’t know that codebase).

io.text_encoding was added in Python 3.10. I wonder if you have a mix-up of Python 3.10 and < 3.10 packages? What does sys.path say when you look at that? Are you setting PYTHONPATH in your environment and is Python 3.9 (or earlier) on sys.path before Python 3.10?
If you are using Python 3.10, and PYTHONPATH points to 3.9 libraries, then you can get this error.
If so, then the work-around is to make sure that PYTHONPATH is not set.

1 Like

I think you are confusing the PATH environment variable of the OS and the python sys.path.

Why would you think that? Of course, I sometimes goof up, but in the 24 years that I have been using Python I don’t think I ever confused env variables PATH and PYTHONPATH :slight_smile: When PYTHONPATH (not PATH, PATH is not relevant here) is set to a particular path, then this will appear in sys.path as the first item after the empty string.
See the Python docs at sys — System-specific parameters and functions — Python 3.12.0 documentation

When PYTHONPATH is set to a python3.9 library path (e.g. “/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9” on MacOS) but python 3.10 is actually used, the reported error is easily reproducible (well, a very similar error at least).
Note that in my earlier post I was explicitly talking about Python 3.9 libraries on the PYTHONPATH, and about “using Python 3.10”. By looking at sys.path the OP would be able to verify whether or not PYTHONPATH is really causing trouble here or if yet sth else is going on.

The thing that is strange in the original post, and that I don’t understand and cannot reproduce by tweaking sys.path, is that the error message says “unknown location”:

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

So, this error (with “unknown location”) can only be triggered if the package path of the “_io” module is NULL or if it’s not a valid unicode string (see: https://github.com/python/cpython/blob/main/Python/ceval.c#L2658). Since the _io module is builtin, _io.__file__ is not defined, so that might explain the error message (what is kind of confusing in the error message is that it refers to module “io” rather than “_io”, but _io.__name__ is also “io” rather than “_io”!).

Going out on a limb, I’d say that this suggests that sth is wrong in the Python virtual environment that the OP is using (or in the Python install itself, since the OP doesn’t seem to be using any virtual env); probably sth similar to a mix up of libraries of different Python versions. It could also be caused by somehow trying to use a system python executable (3.10) with libraries from a virtual env for 3.9 - which can easily lead to lots of trouble. One fix then would be:

  • set up a new, proper virtual env for Python 3.10, reinstall everything in that env, and try again
  • make sure to not use the system wide python, but the executable inside the virtual env
  • make sure PYTHONPATH is not set and that PYTHONHOME is also not set when trying to reproduce it
1 Like

Sorry I thought you where trying to say that the wrong version of python was being run. Thank’s for clearing up my confusing that it is about getting old stdlib put in the sys.path for new python version.

2 Likes

I don’t think I understand why the problem comes up. Why would people have the standard library on PYTHONPATH? I thought it was there so people could have their packages in development (and/or driver scripts, pytest etc. for those packages) work with each other without having to install them.

Yeah - that’s a good question… :rofl: I was also not able to completely reproduce the problem by tweaking PYTHONPATH – since if you do so, the error message is not exactly the same. So, I don’t think the original problem is caused/explained by an incorrect PYTHONPATH, but something similar (some mixup of incompatible libraries of different Python versions) is the only cause I can think of.

I have just come up across this error when trying to open another program:

Fatal Python error: init_sys_streams: can't initialize sys standard streams
Python runtime state: core initialized
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/io.py", line 55, in <module>
ImportError: cannot import name 'text_encoding' from 'io' (unknown location)

Apart from the line 54 becoming line 55, the error looks identical to OP’s. Did anyone figure out how to fix this issue? Or is there anyone who came up with a workaround?

I’m running this on a M1 Max Mac, in case that matters…

is this on macOS?
Where did you install python from?

Yes. Also, before you ask, my Python distributions are all up to date.

It doesn’t seem to matter where I installed Python from. I have tried running this program with Python distributions installed through Macports, Homebrew and Miniconda and none of them have worked.

It would seem that the problem is that all these installs are interfering with each other. Check what environment variables you have set that change python’s behaviour in you shell. What do you see output from the env command in a terminal .

There’s a lot of output from the env command. I am assuming you’re referring to the $PYTHONPATH environment? I’ve checked which versions of python are active (e.g., which python, python --version, etc.) and done my best to make sure that the correct libraries are being pointed to.

I uninstalled my Miniconda distribution because I don’t need it for any dependencies, but that still doesn’t help.

Check for anything that has python in its name or python in its value.

Try this this to focus on candidates to review env | grep -i python

Yeah, but what exactly do you want to know? It’s in my $PATH and my PYTHONPATH environments:

PATH=/Users/***/Library/orca_6_0_0:/opt/local/include:/opt/local/lib:/opt/local/share:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Library/TeX/texbin:/Applications/orca/orca_5_0_4_macosx_arm64_openblas_openmpi411:/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/idlelib:/usr/bin/java:/Applications/gautoir:/Users/***/.cargo/bin:/Users/***/bin/dislin/bin
PYTHONPATH=:/opt/local/Library/Frameworks/Python.framework/Versions/3.12/bin:/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12:/opt/local/bin

which python gives me /opt/local/bin/python
which pip gives me /opt/local/bin/pip