不灭的焱

革命尚未成功,同志仍须努力

作者:php-note.com  发布于:2021-10-11 02:06  分类:Linux软件安装/优化  编辑

CentOS7的Python包管理(1) 卸载Python3

CentOS7的Python包管理(2) pip的删除及安装(配置国内Python包镜像)

CentOS7的Python包管理(3) 安装Python2和Python3

CentOS7的Python包管理(4) 安装pyenv(Python多版本管理)

CentOS7的Python包管理(5) 安装virtualenv、virtualenvwrapper(Python虚拟环境管理)

通过以上文章的学习,终于知道如何管理Python多版本和依赖包的隔离问题,下面记录一下自己在CentOS7环境下管理Python环境的笔记:

一、系统(自带)环境

(1) CentOS7.9,Python 2.7.5,Git 1.8.3.1

[root@CentOS7-1 ~]# cat /etc/redhat-release  
CentOS Linux release 7.9.2009 (Core)
[root@CentOS7-1 ~]# python -V
Python 2.7.5
[root@CentOS7-1 ~]# git version
git version 1.8.3.1

(2) 阿里云主机,除了“系统盘”外,还单独购买了1块“数据盘”,挂载到目录 /alidata

规划把相关软件安装到目录 /alidata/server/

二、安装pyenv

# 安装依赖库

[root@CentOS7-1 ~]# yum -y groupinstall "Development tools"
[root@CentOS7-1 ~]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

# 安装 pyenv 到目录 /alidata/server/python/pyenv

[root@CentOS7-1 ~]# mkdir -pv /alidata/server/python
mkdir: 已创建目录 "/alidata/server/python"

[root@CentOS7-1 ~]# git clone git://github.com/pyenv/pyenv.git  /alidata/server/python/pyenv
正克隆到 '/alidata/server/python/pyenv'...
remote: Enumerating objects: 20489, done.
remote: Counting objects: 100% (1513/1513), done.
remote: Compressing objects: 100% (607/607), done.
接收对象中:  25% (5123/20489), 1.32 MiB | 122.00 KiB/s  

# 设置环境变量

[root@CentOS7-1 ~]# sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="/alidata/server/python/pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.bash_profile
echo 'eval "$(pyenv init --path)"' >> ~/.bash_profile
 
echo 'export PYENV_ROOT="/alidata/server/python/pyenv"' >> ~/.profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo 'eval "$(pyenv init --path)"' >> ~/.profile
 
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

(1)修改后的 ~/.bash_profile 文件内容为:

[root@CentOS7-1 ~]# vim ~/.bash_profile
# .bash_profile

export PYENV_ROOT="/alidata/server/python/pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
eval "$(pyenv init --path)"

(2)修改后的 ~/.profile 文件内容为:

[root@CentOS7-1 ~]# vim ~/.profile
export PYENV_ROOT="/alidata/server/python/pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

(3)修改后的 ~/.bashrc 文件内容为:

[root@CentOS7-1 ~]# vim ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
eval "$(pyenv init -)"

# 然后 source 一下环境

[root@CentOS7-1 ~]# source  ~/.bash_profile
[root@CentOS7-1 ~]# source  ~/.profile
[root@CentOS7-1 ~]# source  ~/.bashrc

附:新安装的CentOS7环境变量备份:~/.bash_profile,~/.profile,~/.bashrc

# 使用pyenv预安装 Python 2.7.18 和 3.8.12 这两个版本

[root@CentOS7-1 ~]# pyenv install 2.7.18
Downloading Python-2.7.18.tar.xz...
-> https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz
Installing Python-2.7.18...
patching file configure
patching file configure.ac
patching file setup.py
patching file Mac/Tools/pythonw.c
patching file setup.py
patching file Doc/library/ctypes.rst
patching file Lib/test/test_str.py
patching file Lib/test/test_unicode.py
patching file Modules/_ctypes/_ctypes.c
patching file Modules/_ctypes/callproc.c
patching file Modules/_ctypes/ctypes.h
patching file Modules/_ctypes/callproc.c
patching file setup.py
patching file Mac/Modules/qt/setup.py
patching file setup.py
Installed Python-2.7.18 to /root/.pyenv/versions/2.7.18

[root@CentOS7-1 ~]# pyenv install 3.8.12
Downloading Python-3.8.12.tar.xz...
-> https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tar.xz
Installing Python-3.8.12...
Installed Python-3.8.12 to /root/.pyenv/versions/3.8.12

注意:安装慢的解决办法

mkdir -p /alidata/server/python/pyenv/cache

上传安装包(Python-3.8.12.tar.xz)到此目录,再执行:pyenv install 3.8.12

# 切换至 python 3.8.12

[root@CentOS7-1 ~]# pyenv global 3.8.12 

# 设置pip为国内镜像网站

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

三、安装virtualenv、virtualenvwrapper

# 切换至 python 3.8.12

[root@CentOS7-1 ~]# pyenv global 3.8.12 

# 安装 virtualenv、virtualenvwrapper

[root@CentOS7-1 ~]# python -V          
Python 3.8.12
[root@CentOS7-1 ~]# pip install virtualenv
[root@CentOS7-1 ~]# pip install virtualenvwrapper

# 配置 virtualenvwrapper

此时还不能使用virtualenvwrapper,默认virtualenvwrapper安装在Python安装目录的bin目录下(如:/root/.pyenv/versions/3.8.12/bin/virtualenvwrapper.sh),实际上你需要运行virtualenvwrapper.sh文件才行,先别急,打开这个文件看看,里面有安装步骤,我们照着操作把环境设置好。

(1) 创建目录用来存放虚拟环境

mkdir -pv /alidata/server/python/virtualenvs

(2) 在文件 ~/.bashrc 中,追加如下内容:

# 设置virtualenvwrapper的Python解释器为3.8.12
VIRTUALENVWRAPPER_PYTHON=/alidata/server/python/pyenv/versions/3.8.12/bin/python
# 设置virtualenvwrapper的虚拟环境(父)目录
export WORKON_HOME=/alidata/server/python/virtualenvs
source /alidata/server/python/pyenv/versions/3.8.12/bin/virtualenvwrapper.sh

(3) 重新加载配置文件

source ~/.bashrc

四、使用virtualenvwrapper

此时virtualenvwrapper就可以使用了,virtualenvwrapper的基本使用方式:

1.列出虚拟环境列表

[root@CentOS7-1 ~] workon 
# 或者 
[root@CentOS7-1 ~] lsvirtualenv

2.新建虚拟环境

[root@CentOS7-1 ~] mkvirtualenv [虚拟环境名称]

注意:还可以指定Python版本号

[root@CentOS7-1 ~] mkvirtualenv [虚拟环境名称]  -p /alidata/server/python/pyenv/versions/2.7.18/bin/python
[root@CentOS7-1 ~] mkvirtualenv env_2.7  -p /alidata/server/python/pyenv/versions/2.7.18/bin/python
[root@CentOS7-1 ~] mkvirtualenv env_3.8  -p /alidata/server/python/pyenv/versions/3.8.12/bin/python

3.启动/切换 虚拟环境

[root@CentOS7-1 ~] workon [虚拟环境名称]

4.删除虚拟环境

[root@CentOS7-1 ~] rmvirtualenv [虚拟环境名称]

5.离开虚拟环境,和virutalenv一样的命令

deactivate