当前位置: 首页 > news >正文

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

  1. 安装git
yum install git curl -y
  1. 安装依赖
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    

  1. 创建开发环境普通用户

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 ~]$ 
  1. 查看官方安装脚本找出需要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' 
  1. 使用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
  1. 修改系统环境变量
  • 重要的事情说三遍
  • 这里一定要注意呀,开始只在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

  1. 查看pyenv支持的可用的python版本

[python@C8-196 ~]$ pyenv install -l | less

  1. 创建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
  1. 使用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

相关文章:

  • Win10部署python多版本开发环境
  • Linux常用Shell脚本测试命令
  • Linux常用Shell函数参数
  • Linux脚本shell编程通过数组实现石头剪刀布小游戏
  • CentOS8快速安装Docker
  • CentOS8快速部署轻量级自动化运维平台Spug
  • DveOps常见项目代码部署发布方式
  • 云原生时代一站式DevOps平台--阿里云效
  • IBM WAS 简介
  • 实现判断CentOS的主版本号
  • Linux校验比对文件一致性的shell脚本
  • Linux防火墙之通俗易懂的iptables五表五链解释
  • nmap部分实例应用
  • VSFTP服务器配置具有不同访问权限的虚拟用户
  • linux配置应用服务器通过证书免密码登录SFTP测试站点
  • JavaScript设计模式与开发实践系列之策略模式
  • nginx 负载服务器优化
  • PermissionScope Swift4 兼容问题
  • PV统计优化设计
  • Redis在Web项目中的应用与实践
  • vue:响应原理
  • 百度贴吧爬虫node+vue baidu_tieba_crawler
  • 大快搜索数据爬虫技术实例安装教学篇
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 坑!为什么View.startAnimation不起作用?
  • 蓝海存储开关机注意事项总结
  • 聊聊directory traversal attack
  • 如何使用 JavaScript 解析 URL
  • 使用putty远程连接linux
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #Lua:Lua调用C++生成的DLL库
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (10)STL算法之搜索(二) 二分查找
  • (11)工业界推荐系统-小红书推荐场景及内部实践【粗排三塔模型】
  • (delphi11最新学习资料) Object Pascal 学习笔记---第5章第5节(delphi中的指针)
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (一)基于IDEA的JAVA基础12
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .NET CF命令行调试器MDbg入门(四) Attaching to Processes
  • .net core 6 redis操作类
  • .NET Framework 的 bug?try-catch-when 中如果 when 语句抛出异常,程序将彻底崩溃
  • .net打印*三角形
  • .NET框架
  • /boot 内存空间不够
  • /usr/bin/env: node: No such file or directory
  • /usr/local/nginx/logs/nginx.pid failed (2: No such file or directory)
  • [ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限
  • []指针
  • [Android Studio 权威教程]断点调试和高级调试