title: Msys2 with python 3.8: ImportError: cannot import name 'open_code' from 'io'
body: <p><strong>NOTE: There have been several EDITs to the question, as per comments. They are indicated below, and separated by lines.</strong>
  <strong>As of now, the only remaining issue seems to be that numpy cannot load, possibly (but not certainly) due to two alternative python 3.8 systems present.</strong></p>
  
  <p>I have updated my msys2 system a couple of months ago.
  That apparently included an update from python 3.7 to 3.8, but which left me with two broken pythons: I can start <code>python</code> when it is 3.7, but there are no associated packages, and I cannot start <code>python</code> when it is 3.8, which is the version holding packages.
  I do not know what went wrong with that, or what did I do wrong.
  I just noticed this now with the first time I mean to use python again after the upgrade.</p>
  
  <p>I will describe here a sequence of steps I followed and what I found.
  I will post supporting code below for clarity.</p>
  
  <ol>
  <li><p>I can start python, but <code>pandas</code> (e.g.) and many other packages are not found in python.
  Checking further, <code>/mingw64/lib/python3.7/site-packages</code> is essentially empty (surely emptied when upgrading to 3.8).</p></li>
  <li><p>Looking for the <code>pandas</code> package, I found I have one version installed.</p></li>
  <li><p>The <code>pandas</code> version is for python 3.8, surely upgraded from 3.7.</p></li>
  <li><p>I redirected <code>PYTHONPATH</code> from 3.7 to 3.8</p></li>
  <li><p><del>Now I cannot even start python.</del>
  <strong>EDIT</strong> Now I can start python, with some misconfiguration issues (i.e., partially fixed).</p></li>
  </ol>
  
  <p>Now the question is</p>
  
  <p><del><strong>How can I fix python3.8, which gives the error below?</strong></p>
  
  <p><strong><code>ImportError: cannot import name 'open_code' from 'io' (unknown location)</code></strong>
  </del></p>
  
  <p><strong>How can I fix python3.8, which gives the problems below?</strong></p>
  
  <p>New problems:</p>
  
  <p>5.1. I should have python pointing to 3.8, and also fix packages.</p>
  
  <p>5.2. Some modules are not found, some other are.</p>
  
  <p>Note: I don't know if <a href="https://stackoverflow.com/questions/61264463/msys2-upgrade-breaks-python2-pyqt5">Msys2 upgrade breaks python2-pyqt5</a> has anything to do with this.</p>
  
  <p>Related:</p>
  
  <p><a href="https://github.com/tox-dev/tox/issues/1334" rel="noreferrer">https://github.com/tox-dev/tox/issues/1334</a></p>
  
  <p><a href="https://github.com/yan12125/python3-android/issues/19" rel="noreferrer">https://github.com/yan12125/python3-android/issues/19</a></p>
  
  <p><a href="https://python-forum.io/Thread-Fatal-Python-error-init-sys-streams-can-t-initialize-sys-standard-streams-Attribute" rel="noreferrer">https://python-forum.io/Thread-Fatal-Python-error-init-sys-streams-can-t-initialize-sys-standard-streams-Attribute</a></p>
  
  <hr>
  
  <p><strong>TL;DR</strong>: Supporting code</p>
  
  <ol>
  <li><p><code>pandas</code> not found</p>
  
  <pre><code>$ python
  Python 3.7.4 (default, Jul 11 2019, 10:29:54)
  [GCC 9.1.0] on msys
  Type "help", "copyright", "credits" or "license" for more information.
  Reading /home/user1/.pythonrc
  readline is in /usr/lib/python3.7/lib-dynload/readline.cpython-37m.dll
  &gt;&gt;&gt; import pandas
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  ModuleNotFoundError: No module named 'pandas'
  &gt;&gt;&gt;
  </code></pre></li>
  <li><p><code>pandas</code> is actually installed</p>
  
  <pre><code>$ pacman -Sl | grep python | grep installed
  mingw64 mingw-w64-x86_64-python 3.8.2-1 [installed: 3.8.1-1]
  mingw64 mingw-w64-x86_64-python-apipkg 1.5-1 [installed]
  ...
  mingw64 mingw-w64-x86_64-python-pandas 1.0.3-1 [installed: 1.0.1-1]
  ...
  mingw64 mingw-w64-x86_64-python2-setuptools 44.1.0-1 [installed: 42.0.2-1]
  msys python 3.7.4-1 [installed]
  msys python2 2.7.17-1 [installed]
  </code></pre></li>
  <li><p>My <code>pandas</code> version is for python 3.8</p>
  
  <pre><code>$ pacman -Ql mingw-w64-x86_64-python-pandas | head -5
  mingw-w64-x86_64-python-pandas /mingw64/
  mingw-w64-x86_64-python-pandas /mingw64/lib/
  mingw-w64-x86_64-python-pandas /mingw64/lib/python3.8/
  mingw-w64-x86_64-python-pandas /mingw64/lib/python3.8/site-packages/
  mingw-w64-x86_64-python-pandas /mingw64/lib/python3.8/site-packages/pandas-1.0.1-py3.8.egg-info/
  </code></pre></li>
  <li><p>I redirected <code>PYTHONPATH</code> from 3.7 to 3.8</p>
  
  <p>Changed</p>
  
  <pre><code>export PYVERSION="3.7"
  export PYTHONDIR2="${MINGW_HOME}/lib/python${PYVERSION}"
  export PYTHONPATH="${PYTHONDIR2}:${PYTHONDIR2}/site-packages"
  </code></pre>
  
  <p>to</p>
  
  <pre><code>export PYVERSION="3.8"
  ...
  </code></pre></li>
  <li><p><del>Now I cannot even start python.
  </del>
  <strong>EDIT</strong>: Old problem:</p>
  
  <pre><code>$ python
  Fatal Python error: init_sys_streams: can't initialize sys standard streams
  Traceback (most recent call last):
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/io.py", line 54, in &lt;module&gt;
  ImportError: cannot import name 'open_code' from 'io' (unknown location)
  Aborted (core dumped)
  </code></pre></li>
  </ol>
  
  <p>New problems:</p>
  
  <pre><code>$ python --version
  Python 3.7.4
  $ type python
  python is hashed (/usr/bin/python)
  $ ls /usr/bin/python
  /usr/bin/python
  $ python3.8
  Python 3.8.2 (default, Apr  9 2020, 13:17:39)  [GCC 9.3.0 64 bit (AMD64)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  Reading C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/home/user1/.pythonrc
  Module readline not available.
  Traceback (most recent call last):
    File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/home/user1/.pythonrc", line 42, in &lt;module&gt;
      del os, atexit, readline, rlcompleter, save_history, historyPath
  NameError: name 'readline' is not defined
  &gt;&gt;&gt; import readline
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  ModuleNotFoundError: No module named 'readline'
  &gt;&gt;&gt; import zipfile
  &gt;&gt;&gt; import pandas
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pandas/__init__.py", line 16, in &lt;module&gt;
      raise ImportError(
  ImportError: Unable to import required dependencies:
  numpy: DLL load failed while importing _ctypes: No se puede encontrar el módulo especificado.
  &gt;&gt;&gt; import numpy
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/__init__.py", line 142, in &lt;module&gt;
      from . import core
    File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/core/__init__.py", line 106, in &lt;module&gt;
      from . import _dtype_ctypes
    File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/core/_dtype_ctypes.py", line 25, in &lt;module&gt;
      import _ctypes
  ImportError: DLL load failed while importing _ctypes: No se puede encontrar el módulo especificado.
  &gt;&gt;&gt; exit()
  Error in atexit._run_exitfuncs:
  Traceback (most recent call last):
    File "&lt;frozen importlib._bootstrap&gt;", line 991, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 973, in _find_and_load_unlocked
  ModuleNotFoundError: No module named 'readline'
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT #2</strong>: Adding info as requested.
  I just noticed I don't have <code>pip</code>. That matches the fact that I never installed any package with <code>pip</code>...</p>
  
  <pre><code>$ echo $PATH
  /usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Users/user1/OneDrive/Documents/soft-hard-ware/linux-ubuntu:/c/Users/user1/OneDrive/Documents/soft-hard-ware/linux-ubuntu/rsync:/c/Users/user1/Documents/appls_mydocs/science-math-visualization/gp524-win64-mingw_3/gnuplot/bin:/mingw64/bin
  $ echo $PYTHONPATH
  /c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8:/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages
  $ echo $PYTHONSTARTUP
  /home/user1/.pythonrc
  $ which python3.8
  /mingw64/bin/python3.8
  $ python3.8 -m pip freeze
  C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/bin/python3.8.exe: No module named pip
  $ python3.8 -c "import sys; print(sys.builtin_module_names)"
  ('_abc', '_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale', '_operator', '_signal', '_sre', '_stat', '_string', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', 'atexit', 'builtins', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'msvcrt', 'nt', 'sys', 'time', 'winreg', 'xxsubtype')
  </code></pre>
  
  <p><strong>EDIT #3</strong>:
  Posting as requested, plus additional info.</p>
  
  <pre><code>$ cat .pythonrc
  import os
  print( "Reading " + os.path.realpath(__file__) )
  
  # enable syntax completion
  try:
      import readline
      print( "readline is in " + readline.__file__ )
  except ImportError:
      print("Module readline not available.")
  else:
      import rlcompleter
      readline.parse_and_bind("tab: complete")
  
  # From https://docs.python.org/2/tutorial/interactive.html
  
  # Add auto-completion and a stored history file of commands to your Python
  # interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
  # bound to the Esc key by default (you can change it - see readline docs).
  #
  # Store the file in ~/.pystartup, and set an environment variable to point
  # to it:  "export PYTHONSTARTUP=~/.pystartup" in bash.
  
  import atexit
  import os
  #import readline
  #import rlcompleter
  
  historyPath = os.path.expanduser("~/.pyhistory")
  
  def save_history(historyPath=historyPath):
      import readline
      readline.write_history_file(historyPath)
  
  if os.path.exists(historyPath):
      #import readline
      readline.read_history_file(historyPath)
  
  atexit.register(save_history)
  del os, atexit, readline, rlcompleter, save_history, historyPath
  </code></pre>
  
  <p>I don't see why <code>which python3.8</code> and <code>PYTHONPATH</code> were out of sync:</p>
  
  <pre><code>$ cygpath -w $(which python3.8)
  C:\Users\user1\Documents\appls_mydocs\PortableApps\MSYS2Portable\App\msys32\mingw64\bin\python3.8.exe
  $ echo $PYTHONPATH
  /c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8:/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages
  $ cygpath -w /c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8
  C:\Users\user1\Documents\appls_mydocs\PortableApps\MSYS2Portable\App\msys32\mingw64\lib\python3.8
  $ cygpath -w /c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages
  C:\Users\user1\Documents\appls_mydocs\PortableApps\MSYS2Portable\App\msys32\mingw64\lib\python3.8\site-packages
  $ which python
  /usr/bin/python
  </code></pre>
  
  <p>I seem to have two incomplete/broken python installations (3.7, 3.8).
  I don't know what led to an "incomplete" upgrade.
  A couple of observations (see code below):</p>
  
  <ol>
  <li><p><code>python</code> points to 3.7</p></li>
  <li><p><code>readline</code> is available for 3.7 and not for 3.8.
  I don't know why.</p></li>
  <li><p><code>pandas</code> (and many others) is available for 3.8 and not for 3.7.
  Many of them won't import in 3.8 anyway, due to missing dependencies (which I guess are available in 3.7).
  I don't know why.</p></li>
  <li><p>python3.8 reports the location of <code>.pythonrc</code> in Windows format, while 3.7 reports it in Cygwin format.
  Is that normal?</p></li>
  <li><p>Removing <code>PYTHONPATH</code> does not help.</p></li>
  </ol>
  
  <pre><code>$ unset PYTHONPATH
  $ python3.8
  Python 3.8.2 (default, Apr  9 2020, 13:17:39)  [GCC 9.3.0 64 bit (AMD64)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  Reading C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/home/user1/.pythonrc
  Module readline not available.
  Traceback (most recent call last):
    File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/home/user1/.pythonrc", line 42, in &lt;module&gt;
      del os, atexit, readline, rlcompleter, save_history, historyPath
  NameError: name 'readline' is not defined
  &gt;&gt;&gt; import pandas
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pandas/__init__.py", line 16, in &lt;module&gt;
      raise ImportError(
  ImportError: Unable to import required dependencies:
  numpy: DLL load failed while importing _ctypes: No se puede encontrar el módulo especificado.
  &gt;&gt;&gt; exit()
  Error in atexit._run_exitfuncs:
  Traceback (most recent call last):
    File "&lt;frozen importlib._bootstrap&gt;", line 991, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 973, in _find_and_load_unlocked
  ModuleNotFoundError: No module named 'readline'
  $ python
  Python 3.7.4 (default, Jul 11 2019, 10:29:54)
  [GCC 9.1.0] on msys
  Type "help", "copyright", "credits" or "license" for more information.
  Reading /home/user1/.pythonrc
  readline is in /usr/lib/python3.7/lib-dynload/readline.cpython-37m.dll
  &gt;&gt;&gt; import pandas
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  ModuleNotFoundError: No module named 'pandas'
  &gt;&gt;&gt; exit()
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT #4</strong>:</p>
  
  <p>I checked for all python packages, and I found something strange. It seems the basic python installation comprises two separate packages, one mingw64 and another msys.
  I wouldn't know what does each one do.</p>
  
  <pre><code>$ pacman -Sl | grep "python" | grep "installed"
  ...
  mingw64 mingw-w64-x86_64-python 3.8.2-2 [installed]
  ...
  msys python 3.8.2-1 [installed: 3.7.4-1]
  ...
  </code></pre>
  
  <p>On one hand, there was a mismatch in installed versions mingw64 (3.8.2-2) vs msys (3.7.4-1).
  On the other hand, the subversions available are not exactly the same for both (3.8.2-2) vs (3.8.2-1).
  Anyway, I proceeded upgrading msys python, and this fixed things significantly.</p>
  
  <pre><code>$ pacman -S python
  ...
  $ python
  Python 3.8.2 (default, Apr 16 2020, 15:31:48)
  [GCC 9.3.0] on msys
  Type "help", "copyright", "credits" or "license" for more information.
  Reading /home/user1/.pythonrc
  Traceback (most recent call last):
    File "/home/user1/.pythonrc", line 9, in &lt;module&gt;
      import readline
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/readline.py", line 6, in &lt;module&gt;
      from pyreadline.rlmain import Readline
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pyreadline/__init__.py", line 12, in &lt;module&gt;
      from . import logger, clipboard, lineeditor, modes, console
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pyreadline/clipboard/__init__.py", line 13, in &lt;module&gt;
      from .win32_clipboard import GetClipboardText, SetClipboardText
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pyreadline/clipboard/win32_clipboard.py", line 37, in &lt;module&gt;
      import ctypes.wintypes as wintypes
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/ctypes/wintypes.py", line 20, in &lt;module&gt;
      class VARIANT_BOOL(ctypes._SimpleCData):
  ValueError: _type_ 'v' not supported
  Failed calling sys.__interactivehook__
  Traceback (most recent call last):
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site.py", line 412, in register_readline
      import readline
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/readline.py", line 6, in &lt;module&gt;
      from pyreadline.rlmain import Readline
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pyreadline/__init__.py", line 12, in &lt;module&gt;
      from . import logger, clipboard, lineeditor, modes, console
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pyreadline/clipboard/__init__.py", line 13, in &lt;module&gt;
      from .win32_clipboard import GetClipboardText, SetClipboardText
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pyreadline/clipboard/win32_clipboard.py", line 37, in &lt;module&gt;
      import ctypes.wintypes as wintypes
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/ctypes/wintypes.py", line 20, in &lt;module&gt;
      class VARIANT_BOOL(ctypes._SimpleCData):
  ValueError: _type_ 'v' not supported
  </code></pre>
  
  <p>So I have <code>readline</code> in 3.8 now. But there is another issue that pops up during loading of <code>.pythonrc</code>.</p>
  
  <p>Plus, there is a problem with numpy.
  It does not stem from the python version (<code>/usr/bin/python</code> points now to 3.8).</p>
  
  <pre><code>$ python --version
  Python 3.8.2
  $ python
  ...
  &gt;&gt;&gt; import numpy
  Traceback (most recent call last):
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/core/__init__.py", line 24, in &lt;module&gt;
      from . import multiarray
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/core/multiarray.py", line 14, in &lt;module&gt;
      from . import overrides
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/core/overrides.py", line 7, in &lt;module&gt;
      from numpy.core._multiarray_umath import (
  ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/__init__.py", line 142, in &lt;module&gt;
      from . import core
    File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/core/__init__.py", line 54, in &lt;module&gt;
      raise ImportError(msg)
  ImportError:
  
  IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
  
  Importing the numpy c-extensions failed.
  - Try uninstalling and reinstalling numpy.
  - If you have already done that, then:
    1. Check that you expected to use Python3.8 from "/usr/bin/python.exe",
       and that you have no directories in your PATH or PYTHONPATH that can
       interfere with the Python and numpy version "1.18.3" you're trying to use.
    2. If (1) looks fine, you can open a new issue at
       https://github.com/numpy/numpy/issues.  Please include details on:
       - how you installed Python
       - how you installed numpy
       - your operating system
       - whether or not you have multiple versions of Python installed
       - if you built from source, your compiler versions and ideally a build log
  
  - If you're working with a numpy git repository, try `git clean -xdf`
    (removes all files not under version control) and rebuild numpy.
  
  Note: this error has many possible causes, so please don't comment on
  an existing issue about this - open a new one instead.
  
  Original error was: No module named 'numpy.core._multiarray_umath'
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT #5</strong>
  As per @a_guest suggestion, and pointing me to <a href="https://stackoverflow.com/questions/32608232/valueerror-type-v-not-supported-error-after-installing-pyreadline">&quot;ValueError: _type_ &#39;v&#39; not supported&quot; error after installing PyReadline</a>, I removed <code>pyreadline</code>, and this problem is gone.</p>
  
  <p>Now the only remaining issue seems to be that <code>numpy</code> cannot load, possibly (but not certainly) due to two alternative python 3.8 systems present.</p>
  
  <p>So the questions now is: <a href="https://stackoverflow.com/questions/61378854/msys2-two-python-installations">Msys2: Two python installations?</a></p>