本篇文章主要介绍两种多版本的python管理,大家可以根据自己的需求选择合适的方案
方案一:pyenv
pyenv 是python的开源的多版本管理

https://github.com/pyenv/pyenv
安装
安装相关依赖
yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
安装git
yum install -y git
安装pyenv
下载git
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
配置环境变量 ~/.bashrc
export PATH="/home/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
命令使用
查看可安装版本
pyenv install --list
安装python版本
pyenv install 3.6.6
设置python本地版本
pyenv local 3.6.6
设置全局python版本
pyenv global 2.7.10
卸载
rm -rf $(pyenv root)
安装遇见的问题
1. 如果下载过慢, 通过镜像下载来增加速度
version="2.7.16"; echo $version; wget "https://mirrors.huaweicloud.com/python/$version/Python-$version.tar.xz" -P ~/.pyenv/cache/;pyenv install $version
2.7.16
2. ModuleNotFoundError: No module named '_ctypes
yum install libffi-devel
方案二: conda
开源地址:https://github.com/conda/conda
安装
下载安装脚本
wget -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda2-latest-Linux-x86_64.sh
执行安装
$ sh Miniconda3-latest-Linux-x86_64.sh
# 以下是安装式终端的一些发送信息
Welcome to Miniconda3 py38_4.9.2
In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>
===================================
End User License Agreement - Anaconda Individual Edition
===================================
Copyright 2015-2020, Anaconda, Inc.
All rights reserved under the 3-clause BSD License:
The following packages are included in the repository accessible through Anaconda Individual Edition that relate to cryptography:
openssl
The OpenSSL Project is a collaborative effort to develop a robust, commercial-grade, full-featured, and Open Source toolkit implementing the Transport Layer Security (TLS) and Se
cure Sockets Layer (SSL) protocols as well as a full-strength general purpose cryptography library.
pycrypto
A collection of both secure hash functions (such as SHA256 and RIPEMD160), and various encryption algorithms (AES, DES, RSA, ElGamal, etc.).
pyopenssl
A thin Python wrapper around (a subset of) the OpenSSL library.
kerberos (krb5, non-Windows platforms)
A network authentication protocol designed to provide strong authentication for client/server applications by using secret-key cryptography.
cryptography
A Python library which exposes cryptographic recipes and primitives.
pycryptodome
A fork of PyCrypto. It is a self-contained Python package of low-level cryptographic primitives.
pycryptodomex
A stand-alone version of pycryptodome.
libsodium
A software library for encryption, decryption, signatures, password hashing and more.
pynacl
A Python binding to the Networking and Cryptography library, a crypto library with the stated goal of improving usability, security and speed.
Last updated September 28, 2020
# 是否接受license 直接输入yes 回车
Do you accept the license terms? [yes|no]
[no] >>> yes
Miniconda3 will now be installed into this location:
/root/miniconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/root/miniconda3] >>>
PREFIX=/root/miniconda3
Unpacking payload ...
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /root/miniconda3
.....
Preparing transaction: done
Executing transaction: done
installation finished.
Do you wish the installer to initialize Miniconda3
# 这里直接输入yes
by running conda init? [yes|no]
[no] >>> yes
no change /root/miniconda3/condabin/conda
no change /root/miniconda3/bin/conda
no change /root/miniconda3/bin/conda-env
no change /root/miniconda3/bin/activate
no change /root/miniconda3/bin/deactivate
no change /root/miniconda3/etc/profile.d/conda.sh
no change /root/miniconda3/etc/fish/conf.d/conda.fish
no change /root/miniconda3/shell/condabin/Conda.psm1
no change /root/miniconda3/shell/condabin/conda-hook.ps1
no change /root/miniconda3/lib/python3.8/site-packages/xontrib/conda.xsh
no change /root/miniconda3/etc/profile.d/conda.csh
modified /root/.bashrc
==> For changes to take effect, close and re-open your current shell. <==
If you'd prefer that conda's base environment not be activated on startup,
set the auto_activate_base parameter to false:
conda config --set auto_activate_base false
# 安装完成
Thank you for installing Miniconda3!
配置conda镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
conda config --set show_channel_urls yes
配置环境
安装最新版本python2
# 创建py2环境
conda create -n py2 python=python2
安装最新版本python3
# 检查可用版本
conda search python
# 创建py3 并安装软件
conda create --name py3 python=3.5.4
# conda create -n py3 python=python3 numpy matplotlib
# 进入py3开发环境
activate py3
命令
查询python版本
$conda list
查询可用版本
$conda search python
安装可用版本
$conda install <package-name>
列举当前所有环境
$conda info --envs
进入虚拟环境
$conda activate virtual_env
退出虚拟环境
$conda deactivate
本文详述了使用pyenv和conda进行Python多版本管理的方法,包括安装、配置、版本切换及问题解决,帮助开发者根据需求选择合适方案。

被折叠的 条评论
为什么被折叠?



