Python 语言学习:pyenv、centos下安装、mac下安装

python 语言的版本安装管理工具
更新于: 2023-08-02 13:02:37

安装<后面有快速安装方案>

# mac 下安装记得挂代理 
curl https://pyenv.run | bash
# 测试(pyenv 2.2.4)
pyenv --version
# 经过测试 mac 下经常403,无法安装成功,换个方法
brew install pyenv
brew install pyenv-virtualenv
pyenv install 3.10.0
pyenv global 3.10.0
# 添加全局代理:
git config --global http.proxy http://127.0.0.1:9090
git config --global https.proxy http://127.0.0.1:9090

# 取消全局代理:
git config --global --unset http.proxy
git config --global --unset https.proxy
# 添加相关环境变量<linux一般不会自动添加 pyenv>
export PYENV_ROOT="$HOME/.pyenv";
export PATH="$PYENV_ROOT/bin:$PATH";
export PATH="$PYENV_ROOT/shims:$PATH";
eval "$(pyenv init -)";
eval "$(pyenv virtualenv-init -)";

# 注意这里,mac brew 安装的没有bin目录

.pyenv 本地版本

# 生成 pyenv 文件
pyenv local 3.10.0
安装 python 3.10.0 过程

Mac 下提前安装依赖

brew install openssl readline sqlite3 xz zlib tcl-tk
# 下面的会影响python相关的包安装
brew install mysql-client
brew link mysql-client --force
export PATH="/usr/local/opt/mysql-client/bin:$PATH"

Centos build failed

BUILD FAILED (CentOS Linux 7 using python-build 20180424)

Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

手动安装 openssl 1.1<大部分安装出错的原因都在这里>

  • 检查版本:openssl version
  • 下面的是解决方案
# 1. 下载 openssl
cd /tmp
wget https://www.openssl.org/source/openssl-1.1.1l.tar.gz
tar -zxf openssl-1.1.1l.tar.gz
# 2. 编译安装
cd openssl-1.1.1l
./config --prefix=/usr/local/openssl shared
make && make install
# 3. check 退出再登录即可
/usr/local/bin/openssl version
# 4. 如果找不到命令,则加入这个到 path 中
export PATH="/usr/local/openssl/bin:$PATH";

Mac homebrew 安装 openssl@1.1 

brew update
brew install openssl@1.1
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile
# Verify 
openssl version
# OpenSSL 1.1.1c  28 May 2019
$ pyenv install 3.10.0
Installing Python-3.10.0...
patching file aclocal.m4
patching file configure
Hunk #5 succeeded at 10537 (offset -15 lines).
Installed Python-3.10.0 to /root/.pyenv/versions/3.10.0

检查是否安装正确

# 1. Check that pyenv is in your PATH:
which pyenv
# 2. Check that pyenv's shims directory is in your PATH:
echo $PATH | grep --color=auto "$(pyenv root)/shims"

安装 python3.10.0<走缓存快速安装>

# 准备缓存目录
mkdir -p ~/.pyenv/cache/
cd ~/.pyenv/cache/
# 下载
wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tar.xz --check-certificate=off
# 安装
pyenv install 3.10.0
# 设置为默认
pyenv global 3.10.0
# 有时候可能找不到openssl,用下面的形式<这样可以得到路径:which openssl 得到这个地址 /usr/local/openssl/bin/openssl>
CONFIGURE_OPTS="--with-openssl=/usr/local/openssl" pyenv install -v 3.10.0

常用开发包安装

yum grouplist | grep Development
yum groupinstall "Development Tools"

pyenv 常用操作

操作命令
查看所有可用版本
pyenv install --list
设置默认版本
pyenv global 3.10.0
 安装特定版本
pyenv install 3.10.0
查看已经安装的版本
# 通过目录查看
ls ~/.pyenv/versions/
# 用命令查看
pyenv version
删除特定版本
pyenv uninstall 3.10-dev

国内服务器安装<更快的方案>

git clone https://gitee.com/mirrors/pyenv ~/.pyenv
git clone https://gitee.com/ibopo/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
cd ~/.pyenv && src/configure && make -C src

# 准备缓存目录
mkdir -p ~/.pyenv/cache/
cd ~/.pyenv/cache/
# 下载
wget https://mirrors.huaweicloud.com/python/3.10.0/Python-3.10.0.tar.xz --check-certificate=off
# 安装
pyenv install 3.10.0
# 设置为默认
pyenv global 3.10.0

Ali ubuntu 环境

# 必须的环境
sudo apt update; sudo apt install build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
# 可能报错
apt install libicu70=70.1-2

遇到的问题

sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
Installing Python-3.10.0...
patching file aclocal.m4
patching file configure
Hunk #5 succeeded at 10537 (offset -15 lines).
patching file Misc/NEWS.d/next/Build/2021-10-11-16-27-38.bpo-45405.iSfdW5.rst
patching file configure
patching file configure.ac
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python curses extension was not compiled. Missing the ncurses lib?
WARNING: The Python ctypes extension was not compiled. Missing the libffi lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
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 (Ubuntu 20.04 using python-build 20180424)

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

Last 10 log lines:
Installing collected packages: setuptools, pip
  WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/aric.zheng/.pyenv/versions/3.10.0/include/python3.10/setuptools
  sysconfig: /tmp/python-build.20220602193721.2061645/Python-3.10.0/Include/setuptools
  WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/aric.zheng/.pyenv/versions/3.10.0/include/python3.10/pip
  sysconfig: /tmp/python-build.20220602193721.2061645/Python-3.10.0/Include/pip
  WARNING: The scripts pip3 and pip3.10 are installed in '/home/aric.zheng/.pyenv/versions/3.10.0/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-21.2.3 setuptools-57.4.0
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

同时设置 python2/python3

  • global 命令实现
  • 放前面的会自动为默认版本(python)
# 已经安装了2个版本
pyenv global 3.10.0 pypy2.7-7.3.9
❯ pyenv versions
  system
* 3.10.0 (set by /Users/aric.alo7/.pyenv/version)
* pypy2.7-7.3.9 (set by /Users/aric.alo7/.pyenv/version)

❯ python --version
Python 3.10.0

❯ python3 --version
Python 3.10.0

❯ python2 --version
Python 2.7.18 (8e99af2f9b8e3a58dd8691378a36ef43c3139ee9, Mar 29 2022, 05:16:28)
[PyPy 7.3.9 with GCC Apple LLVM 13.0.0 (clang-1300.0.29.30)]

参考