CentOS8部署多版本共存Python开发环境
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS8部署多版本共存Python开发环境相关的知识,希望对你有一定的参考价值。
Python的版本问题
- 目前企业中使用的Python主要版本是2.x和3.x。
- 官方已经停止2.x的维护,最后一个版本是2.7,但仍有部分项目使用
- 3.x各版本间同样有互不兼容的问题。
CentOS 6 默认Python版本为2.6.6
CentOS 7 默认Python版本为2.7
CentOS 8 默认Python版本为3.6.8
Ubuntu18 默认Python版本为2.7和3.6
Ubuntu20 默认Python版本为3.8
- CentOS 8 上,Python 默认没有被安装。yum使用了一个内部的 Python 二进制文件和库文件。
Python2和3的区别
- 在类和库方面有代差,3.x新增了很多库,2.x没有,2.x有一些库被3.x抛弃了
- 在异常处理方面有语法的改变
- 3.x版本以后统一使用Unicode解决了2.x版本乱码的问题
- 3.x版本中input函数合二为一,不再使用raw_input
- 3.x版本中print变成函数式,以前是语句式
- 3.x版本中/为自然除,整除用//
pyenv实现python多版本共存方案
- 在不适用docker或者kmv等虚拟技术的环境中,为解决pythony多版本安装,我们通常使用github上的pythonenv进行虚拟环境部署。
官网: https://github.com/pyenv/pyenv
CentOS8部署pyenv
- 安装git
yum install git curl -y
- 安装依赖
yum install -y gcc make patch gdbm-devel openssl-devel readline-devel zlib-devel bzip2-devel
...
Upgraded:
cpp-8.5.0-4.el8_5.x86_64 gcc-8.5.0-4.el8_5.x86_64
gcc-c++-8.5.0-4.el8_5.x86_64 libgcc-8.5.0-4.el8_5.x86_64
libgomp-8.5.0-4.el8_5.x86_64 libstdc++-8.5.0-4.el8_5.x86_64
libstdc++-devel-8.5.0-4.el8_5.x86_64 ncurses-6.1-9.20180224.el8.x86_64
ncurses-base-6.1-9.20180224.el8.noarch ncurses-libs-6.1-9.20180224.el8.x86_64
Installed:
bzip2-devel-1.0.6-26.el8.x86_64 gdbm-devel-1:1.18-1.el8.x86_64
ncurses-c++-libs-6.1-9.20180224.el8.x86_64 ncurses-devel-6.1-9.20180224.el8.x86_64
patch-2.7.6-11.el8.x86_64 readline-devel-7.0-10.el8.x86_64
- 创建开发环境普通用户
useradd python;echo -e 'python\\npython' | passwd python
[root@C8-196 ~]# useradd python;echo -e 'python\\npython' | passwd python
Changing password for user python.
New password: BAD PASSWORD: The password is shorter than 8 characters
Retype new password: passwd: all authentication tokens updated successfully.
[root@C8-196 ~]# su - python
[python@C8-196 ~]$
- 查看官方安装脚本找出需要git下来的东西哦
-
由于你懂的原因估计curl不行github
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
-
我们就不到长城非好汉的方式把脚本拷贝下来再执行
checkout "$GITHUB/pyenv/pyenv.git" "$PYENV_ROOT" ## 这个是最基础的,必须得有的
checkout "$GITHUB/pyenv/pyenv-doctor.git" "$PYENV_ROOT/plugins/pyenv-doctor" ## 这个是用于doctor得,可以没有的
checkout "$GITHUB/pyenv/pyenv-installer.git" "$PYENV_ROOT/plugins/pyenv-installer" ## 这个是安装器,必须的把自己下载下来
checkout "$GITHUB/pyenv/pyenv-update.git" "$PYENV_ROOT/plugins/pyenv-update" ## 这个update也得有啊!
checkout "$GITHUB/pyenv/pyenv-virtualenv.git" "$PYENV_ROOT/plugins/pyenv-virtualenv" ## 创建虚拟环境,必须要有
checkout "$GITHUB/pyenv/pyenv-which-ext.git" "$PYENV_ROOT/plugins/pyenv-which-ext" ## 为pyenv提供'push'和'pop'
- 使用git命令一点一点都给下载下来吧
- git路径为https的时候有可能报错,还是使用git://才好
git clone git://github.com/pyenv/pyenv.git ~/.pyenv
git clone git://github.com/pyenv/pyenv-installer.git ~/.pyenv/plugins/pyenv-installer
git clone git://github.com/pyenv/pyenv-update.git ~/.pyenv/plugins/pyenv-update
git clone git://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
git clone git://github.com/pyenv/pyenv-which-ext.git ~/.pyenv/plugins/pyenv-which-ext
- 查看一下下载好的目录内容
[python@C8-196 ~]$ tree -L 1 .pyenv/
.pyenv/
├── bin
├── CHANGELOG.md
├── COMMANDS.md
├── completions
├── CONDUCT.md
├── Dockerfile
├── libexec
├── LICENSE
├── Makefile
├── man
├── plugins
├── pyenv.d
├── README.md
├── src
├── terminal_output.png
└── test
8 directories, 8 files
[python@C8-196 ~]$ tree -L 1 .pyenv/plugins/
.pyenv/plugins/
├── pyenv-installer
├── pyenv-update
├── pyenv-virtualenv
├── pyenv-which-ext
└── python-build
5 directories, 0 files
- 修改系统环境变量
- 重要的事情说三遍
- 这里一定要注意呀,开始只在bashrc里修改不好使,要按照官方的修改方式去改才行
- 这里一定要注意呀,开始只在bashrc里修改不好使,要按照官方的修改方式去改才行
- 这里一定要注意呀,开始只在bashrc里修改不好使,要按照官方的修改方式去改才行
sed -Ei -e '/^([^#]|$)/ a \\
export PYENV_ROOT="$HOME/.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="$HOME/.pyenv"' >> ~/.profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo 'eval "$(pyenv init --path)"' >> ~/.profile
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
- 设置完就好了
[python@C8-196 ~]$ cd projects/web36
[python@C8-196 web36]$ pyenv versions
system
3.10.0
* 3.6.15 (set by /home/python/projects/web36/.python-version)
3.6.15/envs/py3615
3.6.15/envs/venv
3.8.12
py3615
venv
[python@C8-196 web36]$ python -V
Python 3.6.15
[python@C8-196 web36]$ pip -V
pip 18.1 from /home/python/.pyenv/versions/3.6.15/lib/python3.6/site-packages/pip (python 3.6)
使用pyenv安装多版本python
- 查看pyenv支持的可用的python版本
[python@C8-196 ~]$ pyenv install -l | less
- 创建python安装包目录并上传
- 我们可以将已下载好的,需要安装部署的python安装包上传至pyenv中的cache目录中
- 如果没有,可以提前下载好呀
#!/bin/bash
Cache_DIR="~/.pyenv/cache"
[ -p $Cache_DIR ] || mkdir -pv $Cache_DIR
for PYTHON_RV in 2.7.18,3.5.10,3.6.15,3.7.12,3.8.12,3.9.7,3.10.0;do
wget https://www.python.org/ftp/python/"$PYTHON_RV"/Python-"$PYTHON_RV".tar.xz -P ~/.pyenv/cache
done
- 使用pyenv安装多版本python
- 如果cache目录中已有,则直接编译安装
- 如果cache目录中没有,会从官网进行下载后再安装,就是慢
- 查看已有安装包
[python@C8-196 ~]$ cd .pyenv/cache/;ll -h
total 122M
-rw-rw-r-- 1 python python 18M Oct 5 02:34 Python-3.10.0.tar.xz
-rw-rw-r-- 1 python python 24M Oct 5 02:34 Python-3.10.0.tgz
-rw-rw-r-- 1 python python 17M Sep 4 14:21 Python-3.6.15.tar.xz
-rw-rw-r-- 1 python python 22M Sep 4 14:20 Python-3.6.15.tgz
-rw-rw-r-- 1 python python 18M Aug 31 00:55 Python-3.8.12.tar.xz
-rw-rw-r-- 1 python python 24M Aug 31 00:55 Python-3.8.12.tgz
- 使用pyenv install 安装需要的版本
[python@C8-196 cache]$ pyenv install -vvv 3.8.12
/tmp/python-build.20211209133311.16824 ~/.pyenv/cache
/tmp/python-build.20211209133311.16824/Python-3.8.12 /tmp/python-build.20211209133311.16824 ~/.pyenv/cache
Installing Python-3.8.12...
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.8... no
checking for python3... no
checking for python... no
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
...
- 安装完成后查看支持的版本
[python@C8-196 cache]$ pyenv versions
3.10.0
3.6.15
3.8.12
pyenv设置python版本的三种主要方式
- pyenv local ## 对当前目录生效,每个项目一个目录,每个目录设定对应的python版本,实现不同目录,不同版本,互不干扰
[python@C8-196 web36]$ pyenv local --help
Usage: pyenv local <version> <version2> <..>
pyenv local --unset
Sets the local application-specific Python version(s) by writing the
version name to a file named `.python-version'.
When you run a Python command, pyenv will look for a `.python-version'
file in the current directory and each parent directory. If no such
file is found in the tree, pyenv will use the global Python version
specified with `pyenv global'. A version specified with the
`PYENV_VERSION' environment variable takes precedence over local
and global versions.
<version> can be specified multiple times and should be a version
tag known to pyenv. The special version string `system' will use
your default system Python. Run `pyenv versions' for a list of
available Python versions.
Example: To enable the python2.7 and python3.7 shims to find their
respective executables you could set both versions with:
'pyenv local 3.7.0 2.7.15'
- pyenv shell ## 跟当前shell环境有关,临时用一下还行,退出重登后就失效了,不推荐
[python@C8-196 web36]$ pyenv shell --help
Usage: pyenv shell <version>...
pyenv shell -
pyenv shell --unset
Sets a shell-specific Python version by setting the `PYENV_VERSION'
environment variable in your shell. This version overrides local
application-specific versions and the global version.
<version> should be a string matching a Python version known to pyenv.
The special version string `system' will use your default system Python.
Run `pyenv versions' for a list of available Python versions.
When `-` is passed instead of the version string, the previously set
version will be restored. With `--unset`, the `PYENV_VERSION`
environment variable gets unset, restoring the environment to the
state before the first `pyenv shell` call.
- pyenv globle ## 用户得全局设定,不要破坏,不建议使用,如果是root执行,杀伤力就太大了
[python@C8-196 web36]$ pyenv global --help
Usage: pyenv global <version> <version2> <..>
Sets the global Python version(s). You can override the global version at
any time by setting a directory-specific version with `pyenv local'
or by setting the `PYENV_VERSION' environment variable.
<version> can be specified multiple times and should be a version
tag known to pyenv. The special version string `system' will use
your default system Python. Run `pyenv versions' for a list of
available Python versions.
Example: To enable the python2.7 and python3.7 shims to find their
respective executables you could set both versions with:
'pyenv global 3.7.0 2.7.15'
pyenv实现目录绑定python多版本共存
- 创建不同项目目录
mkdir -pv ~/projects/web36,web38,web310
[python@C8-196 cache]$ mkdir -pv ~/projects/web36,web38,web310
mkdir: created directory '/home/python/projects'
mkdir: created directory '/home/python/projects/web36'
mkdir: created directory '/home/python/projects/web38'
mkdir: created directory '/home/python/projects/web310'
- 设置项目目录关联python版本
[python@C8-196 projects]$ cd web36
[python@C8-196 web36]$ pyenv local 3.6.15
[python@C8-196 web36]$ pyenv version
3.6.15 (set by /home/python/projects/web36/.python-version)
[python@C8-196 web36]$ python -V
Python 3.6.15
pyenv虚拟环境实现目录python多版本共存
- 大的公用版本环境,如果安装不同库可能会有冲突
- 为每一个项目都有一个隔离的小环境,需要pyenv配置虚拟环境
- 创建虚拟版本
pyenv virtualenv <可用版本> <虚拟版本名>
- 指定项目目录使用虚拟版本
pyenv local <虚拟版本名>
[python@C8-196 web38]$ pyenv virtualenv 3.8.12 py3812a
Looking in links: /tmp/tmp5utow02w
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.8.12/envs/py3812a/lib/python3.8/site-packages (56.0.0)
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.8.12/envs/py3812a/lib/python3.8/site-packages (21.1.1)
[python@C8-196 web38]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.10.0
3.6.15
3.6.15/envs/py3615
3.6.15/envs/venv
3.8.12
3.8.12/envs/py3812a
py3615
py3812a
venv
[python@C8-196 web38]$ mkdir -pv py3812a,py3812b
mkdir: created directory 'py3812a'
mkdir: created directory 'py3812b'
[python@C8-196 web38]$ pyenv virtualenv 3.8.12 py3812b
Looking in links: /tmp/tmp_kok0cu4
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.8.12/envs/py3812b/lib/python3.8/site-packages (56.0.0)
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.8.12/envs/py3812b/lib/python3.8/site-packages (21.1.1)
[python@C8-196 web38]$ cd py3812a
[python@C8-196 py3812a]$ pyenv local py3812a
(py3812a) [python@C8-196 py3812a]$ python -V
Python 3.8.12
(py3812a) [python@C8-196 py3812a]$ pyenv version
py3812a (set by /home/python/projects/web38/py3812a/.python-version)
(py3812a) [python@C8-196 py3812a]$ cd ../py3812b
[python@C8-196 py3812b]$ pyenv local py3812b
(py3812b) [python@C8-196 py3812b]$ python -V
Python 3.8.12
(py3812b) [python@C8-196 py3812b]$ pyenv version
py3812b (set by /home/python/projects/web38/py3812b/.python-version)
- 至此,多版本共存Python开发环境已经部署完了
- 在此至上可以部署ipython和Jupyter配合开发
pip install ipython jupyter
jupyter notebook --ip=0.0.0.0
以上是关于CentOS8部署多版本共存Python开发环境的主要内容,如果未能解决你的问题,请参考以下文章
[TroubleShooting]CentOS8使用pyenv部署多版本python时报 python: command not found