Linux下安装Python3.6及避坑指南

2019-05-0814:24:16后端程序开发Comments2,478 views字数 1976阅读模式

1.安装依赖环境文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

Python3在安装的过程中可能会用到各种依赖库,所以在正式安装Python3之前,需要将这些依赖库先行安装好。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

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

2. 下载Python3源代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

下载Python3的源代码有两种方式,一种是在它的官网下载,网址如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

[图片]文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

另外一种方式是通过wget直接下载,如以下命令:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

3. 创建安装目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

安装目录可依个人喜好创建,比如在此创建在 /usr/local/python3 :文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

mkdir -p /usr/local/python3文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

4. 解压源码包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

将第2步下载到的源码包进行解压,命令为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

tar -zxvf Python-3.6.1.tgz文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

5. 编译源码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

先进入解压后源码包的目录,再进行配置:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

cd Python-3.6.1
./configure --prefix=/usr/local/python3

之后再编译,然后再安装:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

6. 建立Python3的软链接文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

ln -s /usr/local/python3/bin/python3 /usr/bin/python3

7. 将/usr/local/python3/bin加入PATH文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

编辑bash_profile进行修改环境变量:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

vim ~/.bash_profile文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

在PATH变量下将Python3的启动目录添加进去:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH

改动完毕之后,按Esc,再输入:wq进行保存退出。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

8. 检查Python3及Pip3是否正常可用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

执行如下命令(注意:V是大写的V),如果看到的结果一致的话,说明Python3已经成功安装。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

[alvin@VM_0_16_centos ~]$ python3 -V
Python 3.6.1
[alvin@VM_0_16_centos ~]$ pip3 -V
pip 9.0.1 from /usr/local/lib//site-packages (python 3.6)

避坑指南文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

其实,对于Python3的安装,网络上有太多的帖子了,步骤其实都大同小异。但是,在真正动手安装之后,或多或少都会遇到一些麻烦,特别是对新手而言。下面良许就列举一些常见的坑:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

坑1:configure: error: no acceptable C compiler found in $PATH文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

这个问题就比较简单,就是缺少gcc编译环境。将gcc安装上即可:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

yum install -y gcc文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

当然除此之外,采用源码安装的方式也可以。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

坑2:zipimport.ZipImportError: can't decompress data文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

这种问题就是因为缺少zlib 的相关工具包导致的,将相关依赖包装上即可:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

yum -y install zlib*文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

安装之后再重新编译源码,即可解决。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

坑3:pip3: Can't connect to HTTPS URL because the SSL module is not available文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

这个问题是因为在./configure过程中,如果没有加上–with-ssl参数时,默认安装的软件涉及到ssl的功能不可用,刚好pip3过程需要ssl模块,而由于没有指定,所以该功能不可用。解决办法如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

cd Python-3.6.2
./configure --with-ssl
make
sudo make install

坑4:Multilib version problems文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

这个很明显了,就是同一个库有多个版本。把多余的版本删除了就好。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

首先查询已有的版本(以openssl为例,冲突哪个查哪个)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

# rpm -qa | grep openssl
openssl-devel-1.0.0-27.el6_4.2.x86_64

可以看到系统里安装了和两个版本的openssl,我们留下x86的版本即可:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

rpm --erase --nodeps

再更新一下openssl:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

# yum update "openssl*"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

再查询一下openssl,问题解决!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html

# rpm -qa | grep openssl
openssl-devel-1.0.1e-16.el6_5.7.x86_64
openssl-1.0.1e-16.el6_5.7.x86_64
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/11882.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/11882.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定