小编典典

带有python 3.8的Msys2:ImportError:无法从'io'导入名称'open_code'

python

注意:根据注释,对该问题进行了多次编辑。 它们在下面指示,并用线分隔。
到目前为止,唯一剩下的问题似乎是numpy无法加载,可能(但不是肯定)由于存在两个替代的python 3.8系统。

我几个月前已经更新了我的msys2系统。这显然包括从python
3.7到3.8的更新,但给我留下了两个损坏的python:我可以python在3.7时启动,但是没有相关的软件包,而我python在3.8时无法启动,这是保存软件包的版本。
。我不知道这是怎么了,或者我做错了什么。我现在第一次注意到这是我的第一次意思是升级后再次使用python。

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

  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升级是否中断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 ““, line 1, in
    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"
...
  1. 现在我什至无法启动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
    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.8并且PYTHONPATH不同步:

$ 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

我似乎有两个不完整/损坏的python安装(3.7、3.8)。我不知道是什么导致了“不完整”的升级。一些观察(请参见下面的代码):

  1. python 指向3.7

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

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

  4. python3.8.pythonrc以Windows格式报告的位置,而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
    del os, atexit, readline, rlcompleter, save_history, historyPath
    NameError: name ‘readline’ is not defined

    import pandas
    Traceback (most recent call last):
    File ““, line 1, in
    File “C:/Users/user1/Documents/appls_mydocs/PortableApps/MSYS2Portable/App/msys32/mingw64/lib/python3.8/site-packages/pandas/init.py”, line 16, in
    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 ““, line 991, in _find_and_load
    File ““, 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 ““, line 1, in
    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

所以我现在readline在3.8中。但是在加载时会弹出另一个问题.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之后将我指向
ValueError:type‘v’not
support”错误
,我删除了pyreadline该问题,此问题消失了。

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

现在的问题是:Msys2:两个python安装程序?


阅读 1131

收藏
2021-01-20

共1个答案

小编典典

产生ImportError: cannot import name 'open_code' from 'io' (unknown location)于以下事实:有两个不同版本的Python相互冲突。python仍指向旧版本3.7,但PYTHONPATH已更新为指向新3.8版本。由于文档PYTHONPATH状态,它成为 预先考虑 到模块搜索路径,因此阴影任何内建模块:

默认搜索路径取决于安装,但通常以prefix/lib/pythonversion(请参见PYTHONHOME上文)开头。它总是 附加
PYTHONPATH

您可以通过创建两个不同的虚拟环境来重现该行为,然后在PYTHONPATH指向另一个的同时启动一个虚拟环境。在下文中我用Miniconda创建两个不同的环境中,py37并且py38,分别含有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)
2021-01-20