gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:36:27 27 4
gpt4 key购买 nike

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

几个月前我更新了我的 msys2 系统。
这显然包括从 python 3.7 到 3.8 的更新,但这给我留下了两条坏掉的 python:我可以开始 python 3.7的时候,但是没有关联包,启动不了python当它是 3.8 时,这是包含包的版本。
我不知道这有什么问题,或者我做错了什么。
我现在才注意到这一点,我第一次打算在升级后再次使用 python。

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

  • 我可以启动 python,但是 pandas (例如)和许多其他包在 python 中找不到。
    进一步检查,/mingw64/lib/python3.7/site-packages基本上是空的(升级到 3.8 时肯定是空的)。
  • 寻找pandas包,我发现我安装了一个版本。
  • pandas版本适用于 python 3.8,肯定是从 3.7 升级的。
  • 我重定向了 PYTHONPATH从 3.7 到 3.8
  • 现在我什至无法启动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;博士 : 支持代码
  • 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'
    >>>
  • 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]
  • 我的 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/
  • 我重定向了 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"
    ...
  • 现在我什至无法启动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

    我似乎有两个不完整/损坏的 python 安装(3.7、3.8)。
    我不知道是什么导致了“不完整”升级。
    一些观察(见下面的代码):
  • python指向 3.7
  • readline适用于 3.7 而不适用于 3.8。
    我不知道为什么。
  • pandas (和许多其他)适用于 3.8 而不适用于 3.7。
    由于缺少依赖项(我猜它们在 3.7 中可用),它们中的许多无论如何都不会在 3.8 中导入。
    我不知道为什么。
  • python3.8报告.pythonrc的位置Windows 格式,而 3.7 则以 Cygwin 格式报告。
    这是正常的吗?
  • 删除 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

    所以我有 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 的建议,并将我指向 "ValueError: _type_ 'v' not supported" error after installing PyReadline , 我删除了 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 版本。如 the documentationPYTHONPATH状态,它成为模块搜索路径的前置,因此隐藏任何内置模块:

    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)

    关于python - Msys2 与 python 3.8 : ImportError: cannot import name 'open_code' from 'io' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61028653/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com