无法在运行 Amazon Linux 2023 的新 AWS EC2 实例上使用 pyenv 安装 Python:没有名为:_bz2、_curses、_ctypes、readline、_ssl 的模块

问题描述 投票:0回答:2

我正在尝试在运行 Amazon Linux 2023 的新 AWS EC2 实例上使用 pyenv 安装 Python。我已完成以下步骤:

  1. sudo yum groupinstall "Development Tools"
No match for group package "system-rpm-config"
No match for group package "rcs"
No match for group package "pkgconfig"
  1. sudo yum install gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl11-devel tk-devel libffi-devel xz-devel
    (来自pyenv 文档
Error: Unable to find a match: openssl11-devel
  1. sudo yum install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev
    (来自stackoverflow
Error: Unable to find a match: build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev
  1. sudo yum install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
    (也来自stackoverflow
Error: Unable to find a match: build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl
  1. 遵循本指南
    1. git clone https://github.com/pyenv/pyenv.git ~/.pyenv
    2. echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
    3. echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile 
    4. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
  2. bash --login
    (使用环境变量启动一个新的 shell)
  3. pyenv install 3.11.2

我收到这些错误(tldr,没有名为:_bz2、_curses、_ctypes、readline、_ssl)的模块:

$ pyenv install 3.11.2
Downloading Python-3.11.2.tar.xz...
-> https://www.python.org/ftp/python/3.11.2/Python-3.11.2.tar.xz
Installing Python-3.11.2...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ec2-user/.pyenv/versions/3.11.2/lib/python3.11/bz2.py", line 17, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ec2-user/.pyenv/versions/3.11.2/lib/python3.11/curses/__init__.py", line 13, in <module>
    from _curses import *
ModuleNotFoundError: No module named '_curses'
WARNING: The Python curses extension was not compiled. Missing the ncurses lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ec2-user/.pyenv/versions/3.11.2/lib/python3.11/ctypes/__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
WARNING: The Python ctypes extension was not compiled. Missing the libffi lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'readline'
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ec2-user/.pyenv/versions/3.11.2/lib/python3.11/ssl.py", line 100, in <module>
    import _ssl             # if we can't import it, let the error propagate
    ^^^^^^^^^^^
ModuleNotFoundError: No module named '_ssl'
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

Please consult to the Wiki page to fix the problem.
https://github.com/pyenv/pyenv/wiki/Common-build-problems


BUILD FAILED (Amazon Linux 2023 using python-build 2.3.24)

Inspect or clean up the working tree at /tmp/python-build.20230824022449.134442
Results logged to /tmp/python-build.20230824022449.134442.log

Last 10 log lines:
    LD_LIBRARY_PATH=/tmp/python-build.20230824022449.134442/Python-3.11.2 ./python -E -m ensurepip \
        $ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpeswj7w0u
Processing /tmp/tmpeswj7w0u/setuptools-65.5.0-py3-none-any.whl
Processing /tmp/tmpeswj7w0u/pip-22.3.1-py3-none-any.whl
Installing collected packages: setuptools, pip
  WARNING: The scripts pip3 and pip3.11 are installed in '/home/ec2-user/.pyenv/versions/3.11.2/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-22.3.1 setuptools-65.5.0

完成后,

~/.pyenv/versions/
为空。我不确定这是否与找不到模块有关。然而,似乎在
/tmp/python-build.20230824022449.134442
(或任何日期和时间)中安装了(可能不完整)python 3.11.2。

往里面看,我确实在

/tmp/python-build.20230824022449.134442/Python-3.11.2/Lib
中看到了所谓的缺失模块:

bz2.py
curses
ctypes
ssl.py

只有

readline
似乎不见了。

此外,检查库(根据wiki):

$ openssl version
OpenSSL 3.0.8 7 Feb 2023 (Library: OpenSSL 3.0.8 7 Feb 2023)
$ which openssl
/usr/bin/openssl

我还尝试显式传递 OpenSSL 标头和库的位置:

CONFIGURE_OPTS="-with-openssl=/etc/pki/tls" pyenv install 3.11.2
(根据 wiki

python-3.x linux amazon-web-services amazon-ec2 pyenv
2个回答
1
投票

ChatGPT 解决了这个问题。我所做的就是卸载之前用 yum 安装的所有内容,然后再次安装。我不知道为什么会这样,因为这些包似乎都是一样的:

sudo yum remove gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl11-devel tk-devel libffi-devel xz-devel bzip2-devel ncurses-devel libffi-devel readline-devel openssl-devel zlib-devel xz-devel
sudo yum clean all
sudo yum install gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel tk-devel libffi-devel xz-devel bzip2-devel ncurses-devel libffi-devel readline-devel openssl-devel zlib-devel xz-devel

0
投票

就我而言,我使用自制软件安装了 pyenv 和 openssl@3。上述完全删除的解决方案在某种程度上有所帮助。

  1. 我删除了用自制程序安装的 openssl 和 pyenv。
  2. 按照本文第 5 步中的指定从 git 安装了 pyenv
  3. 命令
    sudo yum remove gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl11-devel tk-devel libffi-devel xz-devel bzip2-devel ncurses-devel libffi-devel readline-devel openssl-devel zlib-devel xz-devel
    对我来说没有成功,因为它尝试删除受保护的包作为
    systemd
    yum
    ,但与openssl相关的包被删除。
  4. 使用
    sudo yum install gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel tk-devel libffi-devel xz-devel bzip2-devel ncurses-devel libffi-devel readline-devel openssl-devel zlib-devel xz-devel
  5. 安装软件包

最后,它对我有用,我能够安装我需要的 Python 3.8.9。 但是我无法安装本文中提到的Python 3.11.2,但我认为这是因为为我安装的openssl版本是

OpenSSL 1.0.2k-fips  26 Jan 2017

© www.soinside.com 2019 - 2024. All rights reserved.