Msys2 with python 3.8: ImportError: cannot import name 'open_code' from 'io'

Msys2 with python 3.8: ImportError: cannot import name 'open_code' from 'io'

注意:根据评论,对该问题进行了多次编辑。它们如下所示,并以线分隔。 截至目前,唯一剩下的问题似乎是 numpy 无法加载,可能(但不一定)由于存在两个替代 python 3.8 系统。

几个月前我更新了我的 msys2 系统。 这显然包括从 python 3.7 到 3.8 的更新,但这让我有两个损坏的 pythons:我可以在 3.7 时启动 python,但没有相关的包,并且我无法启动 python 是 3.8,这是持有包的版本。 我不知道那出了什么问题,或者我做错了什么。 我现在第一次注意到这一点,我打算在升级后再次使用 python。

我将在这里描述我遵循的一系列步骤以及我发现的内容。 为了清楚起见,我将 post 下面的支持代码。

  1. 我可以启动 python,但是 pandas(例如)和许多其他包在 python 中找不到。 进一步检查,/mingw64/lib/python3.7/site-packages 基本上是空的(升级到 3.8 时肯定会清空)。

  2. 寻找pandas包,我发现我安装了一个版本。

  3. pandas 版本适用于 python 3.8,肯定是从 3.7 升级而来的。

  4. 我将 PYTHONPATH 从 3.7 重定向到 3.8

  5. 现在我什至无法开始python。 编辑 现在我可以开始 python,但有一些错误配置问题(即部分修复)。

现在的问题是

如何修复 python3.8,它给出了以下错误?

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

如何修复 python3.8,出现以下问题?

新问题:

5.1。我应该 python 指向 3.8,并修复包。

5.2。有些模块没有找到,有些模块找到了。

注:不知Msys2 upgrade breaks python2-pyqt5是否与此有关

相关:

https://github.com/tox-dev/tox/issues/1334

https://github.com/yan12125/python3-android/issues/19

https://python-forum.io/Thread-Fatal-Python-error-init-sys-streams-can-t-initialize-sys-standard-streams-Attribute


TL;DR: 支持代码

  1. pandas 未找到

    $ 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
    >>> import pandas
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'pandas'
    >>>
    
  2. pandas实际安装

    $ 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]
    
  3. 我的 pandas 版本适用于 python 3.8

    $ 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/
    
  4. 我将 PYTHONPATH 从 3.7 重定向到 3.8

    已更改

    export PYVERSION="3.7"
    export PYTHONDIR2="${MINGW_HOME}/lib/python${PYVERSION}"
    export PYTHONPATH="${PYTHONDIR2}:${PYTHONDIR2}/site-packages"
    

    export PYVERSION="3.8"
    ...
    
  5. 现在我什至无法开始python。 编辑:老问题:

    $ 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 <module>
    ImportError: cannot import name 'open_code' from 'io' (unknown location)
    Aborted (core dumped)
    

新问题:

$ 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 <module>
    del os, atexit, readline, rlcompleter, save_history, historyPath
NameError: name 'readline' is not defined
>>> import readline
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'readline'
>>> import zipfile
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pandas/__init__.py", line 16, in <module>
    raise ImportError(
ImportError: Unable to import required dependencies:
numpy: DLL load failed while importing _ctypes: No se puede encontrar el módulo especificado.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/__init__.py", line 142, in <module>
    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 <module>
    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 <module>
    import _ctypes
ImportError: DLL load failed while importing _ctypes: No se puede encontrar el módulo especificado.
>>> exit()
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'readline'

编辑#2:按要求添加信息。 我刚刚注意到我没有 pip。这与我从未使用 pip...

安装任何软件包的事实相符
$ 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')

编辑#3: 按要求张贴,加上附加信息。

$ 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

我不明白为什么 which python3.8PYTHONPATH 不同步:

$ 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

我似乎有两个 incomplete/broken python 安装(3.7、3.8)。 我不知道是什么导致 "incomplete" 升级。 一些观察(见下面的代码):

  1. python指向3.7

  2. readline 适用于 3.7 而不适用于 3.8。 不知道为什么。

  3. pandas(以及许多其他)可用于 3.8 而不是 3.7。 由于缺少依赖项(我猜在 3.7 中可用),它们中的许多无论如何都不会在 3.8 中导入。 不知道为什么。

  4. python3.8 以 Windows 格式报告 .pythonrc 的位置,而 3.7 以 Cygwin 格式报告。 正常吗?

  5. 删除 PYTHONPATH 没有帮助。

$ 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 <module>
    del os, atexit, readline, rlcompleter, save_history, historyPath
NameError: name 'readline' is not defined
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pandas/__init__.py", line 16, in <module>
    raise ImportError(
ImportError: Unable to import required dependencies:
numpy: DLL load failed while importing _ctypes: No se puede encontrar el módulo especificado.
>>> exit()
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", 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
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>> exit()

编辑 #4:

我检查了所有 python 个包,发现了一些奇怪的东西。似乎基本 python 安装包括两个独立的包,一个 mingw64 和另一个 msys。 我不知道每个人是做什么的。

$ 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]
...

一方面,安装的版本 mingw64 (3.8.2-2) 与 msys (3.7.4-1) 不匹配。 另一方面,(3.8.2-2) 和 (3.8.2-1) 的可用子版本并不完全相同。 不管怎样,我继续升级 msys python,这对问题有很大的影响。

$ 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 <module>
    import readline
  File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/readline.py", line 6, in <module>
    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 <module>
    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 <module>
    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 <module>
    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 <module>
    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 <module>
    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 <module>
    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 <module>
    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 <module>
    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 <module>
    class VARIANT_BOOL(ctypes._SimpleCData):
ValueError: _type_ 'v' not supported

所以我现在在 3.8 中有 readline。但是在 .pythonrc.

的加载过程中会弹出另一个问题

另外,numpy 有问题。 它不是源于 python 版本(/usr/bin/python 现在指向 3.8)。

$ python --version
Python 3.8.2
$ python
...
>>> 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 <module>
    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 <module>
    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 <module>
    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 "<stdin>", line 1, in <module>
  File "/c/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/numpy/__init__.py", line 142, in <module>
    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 <module>
    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'

编辑 #5 根据@a_guest 的建议,并指向 ,我删除了 pyreadline,这个问题就消失了。

现在唯一剩下的问题似乎是 numpy 无法加载,可能(但不一定)由于存在两个替代 python 3.8 系统。

所以现在的问题是:Msys2: Two python installations?

ImportError: cannot import name 'open_code' from 'io' (unknown location) 是因为有两个不同版本的 Python 相互冲突。 python 仍然指向旧版本 3.7,但 PYTHONPATH 已更新为指向新的 3.8 版本。正如 PYTHONPATHthe documentation 所述,它成为模块搜索路径的 prepended,因此隐藏了任何内置模块:

The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.

您可以通过创建两个不同的虚拟环境来重现该行为,然后启动一个,同时让 PYTHONPATH 指向另一个。下面我使用 Miniconda 创建了两个不同的环境,py37py38,分别包含 3.7 和 3.8 安装。

(py37) user@pc:~$ python --version
Python 3.7.6
(py37) user@pc:~$ PYTHONPATH=~/miniconda3/envs/py38/lib/python3.8/ python
Fatal Python error: init_sys_streams: can't initialize sys standard streams
Traceback (most recent call last):
  File "/home/user/miniconda3/envs/py38/lib/python3.8/io.py", line 54, in <module>
ImportError: cannot import name 'open_code' from 'io' (unknown location)
Aborted (core dumped)