Python版本升级以及版本切换

阅读: 1035 | 文章分类: Python | Tag: Python  

上篇文章 [[初识Python-工具介绍篇](https://www.cloudhy.cn/article/101)] 里面我提到,Python2+和Python3+两个版本是互不兼容的,所以要使用Python3+的版本需要做升级或者使用其他工具切换版本,这里我就从版本升级和版本切换两个方面讲起 本次环境均使用CentOS7.5.1804最小化安装操作系统。 # 版本升级 ### yum安装升级方法 [3.6版本] yum安装的话相对比较容易些,直接yum安装即可,不过需要使用网络yum源,还有yum安装只支持到3.6版本 使用网络yum源我就不多说了,需要的话在我网站搜索yum找到rpm与yum使用...那篇文章 安装epel源 ``` yum -y install epel-release ``` 安装 ``` yum -y install python36 ``` 安装完成之后我们需要将/usr/bin/python的文件修改下软链 删除原有软链 ``` mv /usr/bin/python /tmp/ ``` 创建新的软链 ``` ln -s /usr/bin/python3.6 /usr/bin/python ``` 更换完成后查看版本 ``` [root@Server-1 ~]# python -V Python 3.6.3 ``` ### 源码包升级 [3.7版本] 先安装下载和编译所需软件 ``` yum -y install wget gcc ``` 下载源码包 ``` wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz ``` 解压源码包 ``` tar xf Python-3.7.0.tgz ``` 进入目录内进行编译安装 ``` cd Python-3.7.0 ./configure make make install ``` 修改原有python命令软链 ``` mv /usr/bin/python /tmp/ ln -s /usr/local/bin/python3.7 /usr/bin/python ``` 更换完成后查看版本 ``` [root@Server-2 ~]# python -V Python 3.7.0 ``` > 因为系统部分功能仍然需要依赖自带的python版本,所以没有剔除系统自带版本,通过修改软链即可。正在使用的生成环境中建议不要轻易修改,请根据环境业务做考量后再进行修改,否则会出现各类不可预料的问题!原有版本还在,如需恢复只需要修改回软链即可,在/usr/bin/目录下仍有旧版本,同样方式做软链即可。 # 版本切换 版本切换需要借助GitHub上的开源软件pyenv [pyenv项目地址](https://github.com/pyenv/pyenv-installer) 在安装之前,我们需要安装git然后才能将git上的源码download下来,另外还需要安装好几个依赖包后面在pyenv里安装python版本时候用到 ``` yum -y install git gcc make patch gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel readline-devel libffi-devel ``` 执行作者提供的安装方式安装 ``` curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash ``` 设置环境变量 ``` vim ~/.bashrc # 在最后面添加下面内容 export PATH="~/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" ``` 设置完环境变量记得source一下配置文件,不然得下次登陆才生效 ``` source .bashrc 或 . .bashrc ``` 到这里就已经完成安装了 列出所有版本 ``` pyenv install --list ``` 查看当然所有版本 ``` [root@Server-2 ~]# pyenv versions * system (set by /root/.pyenv/version) 3.7.0 ``` > 因为我还没添加任何版本所以列出来没有任何版本 安装版本 ``` pyenv install 3.7.0 ``` 列出所有版本时前面只有版本号那些都是python的版本,所以安装时直接接版本对应你要的版本号即可 卸载版本 ``` pyenv uninstall 3.7.0 ``` 接下来你就可以愉快地自由玩耍啦~ # pyenv安装python版本问题 ### 问题一 ``` [root@Server-3 ~]# pyenv install 3.7.0 Downloading Python-3.7.0.tar.xz... -> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz Installing Python-3.7.0... BUILD FAILED (CentOS Linux 7 using python-build 1.2.6) Inspect or clean up the working tree at /tmp/python-build.20180720220743.2553 Results logged to /tmp/python-build.20180720220743.2553.log Last 10 log lines: File "/tmp/python-build.20180720220743.2553/Python-3.7.0/Lib/ensurepip/__main__.py", line 5, in <module> sys.exit(ensurepip._main()) File "/tmp/python-build.20180720220743.2553/Python-3.7.0/Lib/ensurepip/__init__.py", line 204, in _main default_pip=args.default_pip, File "/tmp/python-build.20180720220743.2553/Python-3.7.0/Lib/ensurepip/__init__.py", line 117, in _bootstrap return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths) File "/tmp/python-build.20180720220743.2553/Python-3.7.0/Lib/ensurepip/__init__.py", line 27, in _run_pip import pip._internal zipimport.ZipImportError: can't decompress data; zlib not available make: *** [install] 错误 1 ``` 如果遇到上面这个错误,那么你是缺少了两个包(一般是缺少了zlib-devel) #### 解决方法 安装zlib zlib-devel两个包即可 ``` yum -y install zlib zlib-devel ``` --- ### 问题二 ``` [root@Server-3 ~]# pyenv install 3.7.0 Downloading Python-3.7.0.tar.xz... -> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz error: failed to download Python-3.7.0.tar.xz BUILD FAILED (CentOS Linux 7 using python-build 1.2.6) ``` #### 解决方法 这个问题是无法下载版本,估计是你网络问题,所以检查网络,ping下 www.python.org 这个网址看能不能通,然后再重新执行安装命令 --- ### 问题三 ``` [root@Server-3 ~]# pyenv install 3.7.0 Downloading Python-3.7.0.tar.xz... -> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz Installing Python-3.7.0... BUILD FAILED (CentOS Linux 7 using python-build 1.2.6) Inspect or clean up the working tree at /tmp/python-build.20180720225608.15094 Results logged to /tmp/python-build.20180720225608.15094.log Last 10 log lines: import pip._internal File "/tmp/tmpjy17wffh/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/__init__.py", line 42, in <module> File "/tmp/tmpjy17wffh/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/cmdoptions.py", line 16, in <module> File "/tmp/tmpjy17wffh/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 25, in <module> File "/tmp/tmpjy17wffh/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/download.py", line 39, in <module> File "/tmp/tmpjy17wffh/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/utils/glibc.py", line 3, in <module> File "/tmp/python-build.20180720225608.15094/Python-3.7.0/Lib/ctypes/__init__.py", line 7, in <module> from _ctypes import Union, Structure, Array ModuleNotFoundError: No module named '_ctypes' make: *** [install] 错误 1 ``` 找不到_ctypes模块,这是因为没有安装libffi-devel导致 #### 解决方法 ``` yum -y install libffi-devel ``` --- ### 问题四 ``` [root@Server-3 ~]# pyenv install 3.7.0 Downloading Python-3.7.0.tar.xz... -> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz Installing Python-3.7.0... WARNING: The Python bz2 extension was not compiled. Missing the bzip2 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 (CentOS Linux 7 using python-build 1.2.6) Inspect or clean up the working tree at /tmp/python-build.20180720232459.27494 Results logged to /tmp/python-build.20180720232459.27494.log Last 10 log lines: install|*) ensurepip="" ;; \ esac; \ ./python -E -m ensurepip \ $ensurepip --root=/ ; \ fi Looking in links: /tmp/tmp2wvgaw3j Collecting setuptools Collecting pip Installing collected packages: setuptools, pip Successfully installed pip-10.0.1 setuptools-39.0.1 ``` 这里就已经有比较明显的错误提示了,上面提示警告没有bzip2、GNU,错误:没有Openssl #### 解决方法 ``` yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel readline-devel libffi-devel ``` 注意:升级完成后yum通常会出现无法使用的情况,[解决方法](https://www.cloudhy.cn/article/103 "解决方法")